MVC: How Do I RedirectToAction While Passing a Parameter?

April 1, 2008 12:01 AM

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"
     }
));

Tags: ,
Categories: MVC
Actions: E-mail | Permalink | Comments (2) RSS Feed for this post's comments.

Comments

4/2/2008 7:40 PM #

tgmdbm

there's another syntax but I'm not sure I like it as much.

RedirectToAction(
new RouteValueDictionary {
{"controller", "Blah"},
{"action", "Blah"},
{"foo", "Bar"}
}
);

tgmdbm gb

4/3/2008 7:36 AM #

Troy Goode

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.

Troy Goode us

Add Comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading




Troy Goode

Troy Goode
Microsoft Certified Professional Developer
AddThis Feed Button

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in  anyway.

© Copyright 2008

Colophon

Powered by:
BlogEngine.NET 1.4
Template:
Designs by Darren
Header Font:
Stamper
Syntax Highlighting:
WLW Code Snippet Plugin