Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RedirectToAction with parameter

I have an action I call from an anchor thusly, 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.

like image 445
Eric Brown - Cal Avatar asked Aug 10 '09 22:08

Eric Brown - Cal


People also ask

How do I pass multiple parameters in RedirectToAction?

To pass multiple values to the new controller method, set TempData values and/or pass them as parameters. First, add keyword/value pairs to the TempData collection to pass any number of values to the view. The temp data collection is persisted across controller method calls.

Can we pass model in RedirectToAction?

By including a NuGet package called MvcContrib you can easily pass model or form data through a simple RedirectToAction function call inside of your controllers. The data will then be handled in the other view and be strongly typed to the model you are passing through the redirect.


1 Answers

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

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

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

like image 105
Kurt Schindler Avatar answered Sep 16 '22 14:09

Kurt Schindler