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

Kick It on DotNetKicks.comShout It on DotNetShoutOuts.com

4 Responses to “MVC: How Do I RedirectToAction While Passing a Parameter?”

  1. tgmdbm says:

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

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

  2. Troy Goode says:

    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.

  3. Syed Tameemuddin says:

    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

  4. Jim F says:

    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