MVC: How Do I RedirectToAction While Passing a Parameter?
// April 1st, 2008 // MVC
A common scenario that seems underdocumented to me in the latest MVC release, is how does one use RedirectToAction while passing a value to that action?
In the December CTP, you would do this:
RedirectToAction( new{
controller = "Blah",
action = "Blah",
foo = "Bar"
} );
The new syntax is close, but not easily discoverable. Instead of the above, from now on you should use:
RedirectToAction( new RouteValueDictionary(
new{
controller = "Blah",
action = "Blah",
foo = "Bar"
}
));




there’s another syntax but I’m not sure I like it as much.
RedirectToAction(
new RouteValueDictionary {
{”controller”, “Blah”},
{”action”, “Blah”},
{”foo”, “Bar”}
}
);
Nice point, James. While I agree that that syntax isn’t quite as nice, I can see where the string-based route value construction could be useful for a more dynamic scenario. Especially if you were doing MVC using the DLR and IronRuby or whathaveyou.
Sir,
Can U Please provide me with an example, how i will be able to pass the parameters to different ViewPage without ViewData[], and it should be a RedirecttoAction() Method, i dont want to use Sessions
Would this work?
ReDirectToAction( “Blah”,new{foo = “bar”, morefoo=”morebar”})
protected void RedirectToAction(
string action,
NameValueCollection queryStringParameters
)
Parameters
action
The action name
queryStringParameters
Query string entries