Mike Bosch's Blog on .NET

Agile enterprise architecture in .NET, SOA, WCF, WS-*, AJAX, MVC, Sharepoint and more...

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

Posted: Feb 02 2008, 02:30 AM by MikeBosch | with 11 comment(s) |
Filed under: ,

Comments

Luke Venediger said:

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

# February 7, 2008 5:34 PM

Mike Bosch said:

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!

# February 11, 2008 5:40 PM

Mike Bosch said:

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

# February 11, 2008 5:44 PM

Martin said:

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?

# April 17, 2008 2:45 PM

Guillermo said:

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

}

# January 2, 2009 6:38 AM

cudude said:

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

# September 23, 2009 10:51 PM

cudude said:

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

# September 24, 2009 8:02 AM

Tommy Albinsson said:

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

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

Cheers.

# October 30, 2009 10:08 AM

AP said:

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

# December 24, 2009 8:53 AM

max_voronitskiy said:

<div id="menu">

   <ul class="menu">

       <li><span><%=Html.ActionLink("Home", "Home", "Home")%> </span></li>

       <li><span><%=Html.ActionLink("Inventory", "Manage", "Inventory")%></span>

           <ul>

               <li><span><%=Html.ActionLink("Manage Inventory", "Manage", "Inventory")%></span></li>

               <li><span><%=Html.ActionLink("Add Existent", "AddExistent", "Inventory")%></span></li>

           </ul>

       </li>

       <li><span><a href="#">Support</a></span></li>

       <li><span> <%= Html.ActionLink("Sign out", "LogOff", "Account")%></span></li>

   </ul>

</div>

# January 3, 2010 3:57 PM