RedirectToAction with parameter

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: 9809
Total Answered: 2
Total Marked As Answer: 0
Posted On: 18-May-2015 05:00

Share:   fb twitter linkedin
694 Points
92 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
224 Points
52 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