How to save the ModelState into session following the good practice<!-- --> | <!-- -->Patrick Desjardins Blog
Patrick Desjardins Blog
Patrick Desjardins picture from a conference

How to save the ModelState into session following the good practice

Posted on: April 21, 2012

Tim Barcz, Matt Hawley, Stephen Walther and Scott Guthrie (VP at Microsoft and lead for many project like Entity Framework, Asp.Net, etc) have already discussed about this problematic and created the PRG pattern to solve this problem. In fact, to solve this problem you should not handle manually the ModelState but simply use Import and Export attribute like the following example.

1[AcceptVerbs(HttpVerbs.Get), ImportModelStateFromTempData]
2public ActionResult MyAction(ModelObject myObject) {
3 return View();
6[AcceptVerbs(HttpVerbs.Post), ExportModelStateToTempData]
7public ActionResult MyActionSubmit(ModelObject myObject) {
8 return View();

These attributes are not from the .Net framework and you need to have them inside your project by creating them. Once it's done once, it's done for the life time of your project.

First, you need to create the attributes. To do, you need to create a class that inherit the class ActionFilterAttribute. Since we are using 2 attributes that share the same information, we will create 3 classes. The first one will contain the sharing key for the session and the two others will be for the Import and Export.

1public abstract class ModelStateTempDataTransfer : ActionFilterAttribute {
2 protected static readonly string Key = typeof(ModelStateTempDataTransfer).FullName;

Then, the class to export. Here you can add more custom code for your project. This version will all the ModelState only if this one contain errors.

1public class ExportModelStateToTempData : ModelStateTempDataTransfer {
2 public override void OnActionExecuted(ActionExecutedContext filterContext) {
3 //Only export when ModelState is not valid
4 if (!filterContext.Controller.ViewData.ModelState.IsValid) {
5 //Export if we are redirecting
6 if ((filterContext.Result is RedirectResult) || (filterContext.Result is RedirectToRouteResult)) {
7 filterContext.Controller.TempData[Key] = filterContext.Controller.ViewData.ModelState;
8 }
9 }
11 base.OnActionExecuted(filterContext);
12 }

The last class will import the ModelState. In fact, it will merge the new one with the old one in the session.

1public class ImportModelStateFromTempData : ModelStateTempDataTransfer {
2 public override void OnActionExecuted(ActionExecutedContext filterContext) {
3 ModelStateDictionary modelState = filterContext.Controller.TempData[Key] as ModelStateDictionary;
5 if (modelState != null) {
6 //Only Import if we are viewing
7 if (filterContext.Result is ViewResult) {
8 filterContext.Controller.ViewData.ModelState.Merge(modelState);
9 } else {
10 //Otherwise remove it.
11 filterContext.Controller.TempData.Remove(Key);
12 }
13 }
15 base.OnActionExecuted(filterContext);
16 }

As you can see, we do not use the session directly but we store everything into the TempData which use the session but handle the life cycle for us. This mean that it won't stay for 20 minutes (default value of a session life time). It will stay until the next post back and be there if the request is redirected.

You can see it in the MVC open source project called MVCContrib (slightly modified version of this one). You can also find the source of the code in this blog post at this website.