For the case imperialx has described, passing the route values is better IMO than using TempData. TempData is fine but when the action you are redirecting to (Index in this case) expects some parameters, then the route values in RedirectToAction are explicit
and clear. Also, the TempData approach wouldn't be suitable at all if the Index action parameters were not nullable (they are in this case but you get the idea). Anyway, go with what works best for you. :-)
imperialx
Member
523 Points
1220 Posts
Passing Parameters using RedirectToAction
Sep 14, 2009 10:09 AM|LINK
Hi,
How do you pass the parameters using RedirectToAction? This one doesn't work [:(]
<% Using Html.BeginForm("AddToCart", "Cart")%>
<%=Html.TextBox("ProductID", item.ProductID)%>
<%=Html.TextBox("returnUrl", ViewContext.HttpContext.Request.Url.PathAndQuery)%>
<input type="submit" value="Submit" />
<% End Using%>
CartController
Function Index(ByVal product As Product, ByVal returnurl As String)
ViewData("returnUrl") = returnurl
Return View(product)
End Function
Public Function AddToCart(ByVal returnUrl As String) As RedirectToRouteResult
...
Return RedirectToAction("Index", returnUrl) '<<<---i want to pass the value of returnUrl to Index action
End Function
hasselhoss
Participant
1201 Points
174 Posts
Re: Passing Parameters using RedirectToAction
Sep 14, 2009 11:15 AM|LINK
Have a look at some of the other RedirectToAction overloads. You can pass a routeValues parameter that will give you what you want. Example:
Return RedirectToAction("Index", new with {.product = product, .returnurl = returnUrl})Alexey Dariy
Member
14 Points
2 Posts
Re: Passing Parameters using RedirectToAction
Sep 14, 2009 05:00 PM|LINK
Pass it through TempData["returnUrl"] instead of ViewData.
those who understand binary, and those who don't.
Augi
Contributor
6730 Points
1142 Posts
Re: Passing Parameters using RedirectToAction
Sep 14, 2009 07:03 PM|LINK
You can pass parameter as GET parameters (as hasselhoss suggested) or using TempData (as Alexey suggested). TempData is better solution in most cases.
http://www.augi.cz/programovani/aspnet-mvc-passing-data-when-redirecting/
hasselhoss
Participant
1201 Points
174 Posts
Re: Passing Parameters using RedirectToAction
Sep 15, 2009 05:44 AM|LINK
For the case imperialx has described, passing the route values is better IMO than using TempData. TempData is fine but when the action you are redirecting to (Index in this case) expects some parameters, then the route values in RedirectToAction are explicit and clear. Also, the TempData approach wouldn't be suitable at all if the Index action parameters were not nullable (they are in this case but you get the idea). Anyway, go with what works best for you. :-)