Passing a model into RedirectToAction()

larryq picture larryq · Dec 19, 2012 · Viewed 8.3k times · Source

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?

Answer

Cloud SME picture Cloud SME · Dec 19, 2012

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.