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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With