Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a model into RedirectToAction()

I'm curious how this works. In MVC you can call View() and pass a model as a parameter, but RedirectToAction (one of its incarnations at least) takes a 'routeValues' object, which appears to be the closest match.

If your model is passed in this parameter will that model type be available in the subsequent action method? Or are there caveats involved that might prevent accurate translation in some circumstances?

like image 435
larryq Avatar asked Dec 19 '12 16:12

larryq


2 Answers

If you need to pass in some-what complex objects to an action after a redirect, you probably want to use either a Session or TempData:

From "What is ASP.NET MVC TempData"

ASP.NET MVC TempData dictionary is used to share data between controller actions. The value of TempData persists until it is read or until the current user’s session times out

By default TempData uses a Session to persist the information, however, as with much of MVC, this is an extensibility point, where you can plug in a Cookie-based provider if you prefer.

like image 93
Cloud SME Avatar answered Sep 20 '22 13:09

Cloud SME


You cannot pass a model object in there but you can pass individual properties that will map to a model in the action that you redirect to.

That works by building up the url to redirect to using the properties, and the model binder in the receiving action.

like image 36
linkerro Avatar answered Sep 19 '22 13:09

linkerro