RedirectToAction with parameter

den
den
2 Points
1 Posts

I have an action I call from an anchor, Site/Controller/Action/ID where ID is an int.

Later on I need to redirect to this same Action from a Controller.

Is there a clever way to do this? Currently I'm stashing ID in tempdata, but when you hit f5 to refresh the page again after going back, the tempdata is gone and the page crashes.

Views: 8207
Total Answered: 2
Total Marked As Answer: 0
Posted On: 18-May-2015 05:00

Share:   fb twitter linkedin
Answers
Brian
Brian
Participant
570 Points
75 Posts
         

Hi den,

You can pass the id as part of the routeValues parameter of the RedirectToAction() method.

 
return RedirectToAction("Action", new { id = 11 });
 

This will cause a redirect to Site/Controller/Action/11. No need for temp or any kind of view data.

 
Posted On: 18-May-2015 05:05
Priya
Priya
Member
62 Points
26 Posts
         

You can also use Keep() method to TempData.

TempData items are removed after they are read. When a key is marked for keep, the key is retained for the next request. As per MSDN TempData.Keep() Marks all keys in the dictionary for retention.

Just call TempData.Keep() before redirection

Posted On: 21-Dec-2015 23:45
 Log In to Chat