Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass object as hidden parameter with RedirectToAction?

Tags:

asp.net-mvc

I have done like this.

public ActionResult GetInfo(SomeModel entity)
{
     ----
     return RedirectToAction("NewAction", "NewController", new System.Web.Routing.RouteValueDictionary(entity));
}

Action which was called

public ActionResult NewAction(SomeModel smodel)
{
     -------
     -------
}

This is working fine but I can see all posted param values on browser address bar, how can I hide these querystring param values in browser.

http://localhost:51545/NewController/NewAction?SurveyID=13&CatID=1&PrimaryLang=1&SurveryName=Test%20Survery&EnableMultiLang=False&IsActive=False

Any Help will be appreciated.

like image 867
10K35H 5H4KY4 Avatar asked Aug 27 '14 07:08

10K35H 5H4KY4


People also ask

Can we pass model in RedirectToAction?

Finally, the PersonModel class object is passed to the RedirectToAction method along with the name of the destination Controller and its Action method. The Controller consists of the following Action method. Inside this Action method, the PersonModel class object is received.

What is the difference between RedirectToAction () and RedirectToRoute () in MVC?

RedirectToAction will return a http 302 response to the browser and then browser will make GET request to specified action. Save this answer. Show activity on this post. Ideally I would use RedirectToRoute for Action Links/Images and RedirectToAction in Controller's Action to redirect to another Controller's Action .

Which is correct syntax for RedirectToAction?

RedirectToAction(String, String, RouteValueDictionary) Redirects to the specified action using the action name, controller name, and route values.


1 Answers

In your case instead of using RouteValueDictionary and passing model from querystring try TempData(because when we use RedirectToAction it will make a new http request and object routes will be shown in url so its not a good approach to display sensitive data in url).

Use TempData as shown :-

public ActionResult GetInfo(SomeModel entity)
{
  ----
  TempData["entity"] = entity; //put it inside TempData here
  return RedirectToAction("NewAction", "NewController");
}

public ActionResult NewAction()
{
   SomeModel smodel = new SomeModel();
   if(TempData["entity"] != null){
   smodel = (SomeModel)TempData["entity"];  //retrieve TempData values here
   }
   -------
   -------
}

The benefit of using TempData here is that it will retain its value for one redirect and moreover model will be privately transported to another controller action and once you read data from TempData its data will be disposed automatically and if you want to retain TempData value after reading it then use TempData.keep("entity").


OR

If your Views are in a same Controller then this a simple solution for your problem :

public ActionResult GetInfo(SomeModel entity)
{
  ----
  return NewAction(entity);
}

public ActionResult NewAction(SomeModel smodel)
{
   -------
   -------
  return View("NewAction",smodel)
}

As Correctly Commented by @Chips_100 so i m including it here :- The first solution will do a real redirect (302) which will update the URL in the users browser. The second solution will give the desired result while keeping the original URL in the address bar.

like image 104
Kartikeya Khosla Avatar answered Oct 12 '22 08:10

Kartikeya Khosla