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

Scenario:

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)...

Solution:

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...

9 Comments

  • 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:

    RedirectToAction(new
    {
    action = "Display",
    smurfId = smurf.ID,
    smurf = smurf
    });

    My Display controller method looks like this

    public void Display(int? id, Smurf smurf)
    {
    ...
    }

    Thanks,
    Luke

  • 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:
    "home/display/319/{Namespace.Smurf}"

    Hope this helps!

  • 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;
    RedirectToAction(new
    {
    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...

  • 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?

  • 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);

    }

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

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

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

    RedirectToAction("Index", "Home", new { id = item.id});

    Cheers.










  • Support


Comments have been disabled for this content.