RedirectToAction with parameter
RedirectToAction
with parameter:
return RedirectToAction("Action","controller", new {@id=id});
It is also worth noting that you can pass through more than 1 parameter. id will be used to make up part of the URL and any others will be passed through as parameters after a ? in the url and will be UrlEncoded as default.
e.g.
return RedirectToAction("ACTION", "CONTROLLER", new {
id = 99, otherParam = "Something", anotherParam = "OtherStuff"
});
So the url would be:
/CONTROLLER/ACTION/99?otherParam=Something&anotherParam=OtherStuff
These can then be referenced by your controller:
public ActionResult ACTION(string id, string otherParam, string anotherParam) {
// Your code
}
Kurt's answer should be right, from my research, but when I tried it I had to do this to get it to actually work for me:
return RedirectToAction( "Main", new RouteValueDictionary(
new { controller = controllerName, action = "Main", Id = Id } ) );
If I didn't specify the controller and the action in the RouteValueDictionary
it didn't work.
Also when coded like this, the first parameter (Action) seems to be ignored. So if you just specify the controller in the Dict, and expect the first parameter to specify the Action, it does not work either.
If you are coming along later, try Kurt's answer first, and if you still have issues try this one.
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.