In the RedirectToAction
below, I'd like to pass a viewmodel
. How do I pass the model to the redirect?
I set a breakpoint to check the values of model to verify the model is created correctly. It is correct but the resulting view does not contain the values found in the model properties.
//
// model created up here...
//
return RedirectToAction("actionName", "controllerName", model);
ASP.NET MVC 4 RC
RedirectToAction
returns a 302 response to the client browser and thus the browser will make a new GET request to the url in the location header value of the response came to the browser.If you are trying to pass a simple lean-flat view model to the second action method, you can use this overload of the
RedirectToAction
method.The
RedirectToAction
will convert the object passed(routeValues) to a query string and append that to the url(generated from the first 2 parameters we passed) and will embed the resulting url in the location header of the response.Let's assume your view model is like this
And you in your first action method, you can pass an object of this to the
RedirectToAction
method like thisThis code will send a 302 response to the browser with location header value as
Assuming your
Details
action method's parameter is of typeStoreVm
, the querystring param values will be properly mapped to the properties of the parameter.The above will work for passing small flat-lean view model. But if you want to pass a complex object, you should try to follow the PRG pattern.
PRG Pattern
PRG stands for POST - REDIRECT - GET. With this approach, you will issue a redirect response with a unique id in the querystring, using which the second GET action method can query the resource again and return something to the view.
This will create the url
Store/Details?storeId=101
and in your DetailsGET
action, using the storeId passed in, you will get/build theStoreVm
object from somewhere (from a service or querying the database etc)TempData
Following the PRG pattern is a better solution to handle this use case. But if you don't want to do that and really want to pass some complex data across Stateless HTTP requests, you may use some temporary storage mechanism like
TempData
And read it in your
GET
Action method again.TempData
usesSession
object behind the scene to store the data. But once the data is read the data is terminated.Rachel has written a nice blog post explaining when to use TempData /ViewData. Worth to read.
Using TempData to pass model data to a redirect request in Asp.Net Core
In Asp.Net core, you cannot pass complex types in TempData. You can pass simple types like
string
,int
,Guid
etc.If you absolutely want to pass a complex type object via TempData, you have 2 options.
1) Serialize your object to a string and pass that.
Here is a sample using Json.NET to serialize the object to a string
Now in your
Index
action method, read this value from the TempData and deserialize it to yourCreateUserViewModel
class object.2) Set a dictionary of simple types to TempData
and read it later
I always like to add a property
to my ViewModel (and usually to my Model as well). from there I use
`@Html.Action(@Model.ActionName, someModelRouteValues)
I actually store the "ActionName" in the db entity. That way it's not about the View. It's about the data in the DB entity model (which you can easily manipulate).