Donn Felker

Not A Subscriber?

Join thousands of people who build and refuse to get left behind. One message a week on building, using AI as an accelerator, and staying sharp in the new AI frontier.

Receive one free message a week

February 26, 2010 Donn Felker · updated July 5, 2013

ASPNET MVC2: TempData Now Persists

In MVC1, TempData did not persist:

The value of TempData persists only from one request to the next. (source)

This has changed in MVC2. The TempData dictionary will not persist data until the key is read from the dictionary.

…We’ve changed the implementation in MVC 2 slightly as a result: the value will be removed from TempData after the request in which it is read, so it will continue to exist in your TempData dictionary until you display it in some page. This allows a multi-redirect scenario (such as Windows Live ID login) to use TempData and have it still be hanging around until you’re ready for it. (source)

This is very important to note because if you use MVCContrib’s ModelStateToTempData attribute the values in the modelstate will be persisted across requests as it will be present in the TempData across requests. If you’re using button names as decision criteria in your Http Post  then you might notice that your ModelState is traversing into other actions and showing invalid validation on pages where the model is not of the given type of the model state info that is being presented.