ASP.NET MVC Tip #2 – Redirecting to another action and passing information to it

Saturday, February 2, 2008


Here’s a quick little “how-to” for a pretty common task.  After some controller event, you want to redirect the user to a new action, perhaps on a different controller, while passing some data along to it.  Fair enough (and very simple)…


Within your controller, you would just make a call to the following method:

RedirectToAction(new {controller=”home”, action=”index”, id=931,variable=”abc”});

Of course, you need to have a route configured which can map the “variable” parameter as well.  Mr. Haack has also mentioned that a future release of the MVC framework will consider any parameters not explicity defined in a route will be “overflowed” into query string parameters.  A great idea in my opinion…


  1. http:// says:

    Hey Mike,

    If I had a class called Smurf that had a Foo and Bar property, how would I pass this in a redirect?

    I’ve tried something like:

    action = “Display”,
    smurfId = smurf.ID,
    smurf = smurf

    My Display controller method looks like this

    public void Display(int? id, Smurf smurf)



  2. Mike Bosch says:

    A few things I noticed here. Your action method expects a variable named “id”, but you are setting a variable named “smurfId” in the RedirectToAction method. It should be: id = smurf.ID. The alternative is to create a new route in your Global.asax which maps the smurfId parametet.

    Also, you cannot pass a complex type in the url (on this drop anyways). Your URL will actually end up looking something like this:

    Hope this helps!

  3. Mike Bosch says:

    One more thing. In the case where you want to actually pass the Smurf object to that action, you can store it in the TempData and then the Display action will have access to it.
    TempData["mysmurf"] = smurf;
    action = “Display”,
    smurfId = smurf.ID

    In these cases you probably should pass just the ID, and in the Display action use that ID to load the Smurf object from you repository…

  4. http:// says:

    Great tip. I do have a question for you though. Is there any way to preserve the Request object after calling RedirectToAction that redirects to a different controller, or should I store the values using the TempData object?

  5. http:// says:

    On the beta version works like this:

    private ActionResult RedirectToRoleView(long id)
    //Set the id of as parameter of the redirection Url
    System.Web.Routing.RouteValueDictionary route = new System.Web.Routing.RouteValueDictionary();
    route.Add(“id”, id); //Add Parameter

    return RedirectToAction(“Visitor”, route);


  6. Jon Kruger says:

    If you have MVCContrib you can do it this way using lambda expressions (so it’s strongly typed):

  7. http:// says:

    how do you redirect from a view to a controller action?

  8. http:// says:

    more precisely, how do you redirect to a controller action from an onclick script handler for a view button?

  9. http:// says:

    Above did not work for me. Did it like this instead.

    RedirectToAction(“Index”, “Home”, new { id =});


  10. http:// says:

    I want to try redirect to a page or url using Controller.Redirect(URL) which returns RedirectResult. How to do that?

  11. http:// says:

Leave a Reply