Generic, Session based model binding


Sometimes we need preserve whole model and transfer it across actions or even controllers. Storing model at session good solution for this type of requirements. If we combine this with powerful model binding features of MVC we get elegant way of doing so. We can create generic session based model binding in three easy steps:

Step one: Create model binder

Create a model binder itself. Personally I created SessionDataModelBinder class in /Infrastructure/ModelBinders folder.

using System;
using System.Web.Mvc;

public class SessionDataModelBinder<TModel>
    : IModelBinder
    where TModel : class
    private string SessionKey { get; set; }

    public SessionDataModelBinder(string sessionKey)
        if (string.IsNullOrEmpty(sessionKey))
            throw new ArgumentNullException(nameof(sessionKey));
        SessionKey = sessionKey;

    public object BindModel(
        ControllerContext controllerContext, 
        ModelBindingContext bindingContext)
        // Get model from session
        TModel model = controllerContext
            .Session[SessionKey] as TModel;
        // Create model if it wasn't found from session and store it
        if (model == null)
            model = Activator.CreateInstance<TModel>();
            controllerContext.HttpContext.Session[SessionKey] = model;
        // Return the model
        return model;

Step two: register binder

If we have model like below:

public class ReportInfo
    public int ReportId { get; set; }
    public ReportTypes TypeId { get; set; }

public enum ReportTypes
    Monthly, Yearly

We can register session based model binder for this model in Global.asax in Application_Start method:

protected void Application_Start()

    // Model binders.
    // Remember to specy unique SessionKey
        new SessionDataModelBinder<ReportInfo>("ReportInfo"));

Step three: use it!

Now we can benefit from this model binder simply by adding parameter to our actions:

public class HomeController : Controller
    public ActionResult Index(ReportInfo reportInfo)
        // Simply set properties
        reportInfo.TypeId = ReportTypes.Monthly;

        return View();

    public ActionResult About(ReportInfo reportInfo)
        // reportInfo.TypeId is Monthly now because we set
        // it previously in Index action.
        ReportTypes currentReportType = reportInfo.TypeId;

        return View();