MVC purists may bristle at discussion of State and many would balk at the idea of leveraging Session variables in your MVC application. However, in the real-world, State is necessary. Stateless persistence can often be achieved through use of TempData, ViewData and ViewBack but purists would stress that state should be detached. True, MVC is built on ASP.NET just as WebForms are and it is easy enough to leverage Session variables directly from your controller (eg {Controller}.ControllerContext.HttpContext.Session["SomeKey"]) or from your View (eg <%=Session["SomeKey"] %>)

In order to maintain the MVC pattern state should be leveraged in your Model and only a model should notifies its associated views and controllers when there has been a change in its state. This is where Value Providers come to the rescue. They provide an abstraction over where values the model binder uses actually come from. The framework contains a bunch of built-in value providers like QueryStringValueProvider, FormValueProvider, RouteDataValueProvider and more. In MVC 2 we introduced the

Value Providers have been in MVC since the beginning but in MVC 2 we introduced the ValueProviderFactory and in MVC 3 we introduced the JsonValueProviderFactory used to bind incoming JSON. In this demonstration video we will create a custom ValueProvider we will leverage for Session values.

Original content from Robert Evans; posted by Arvind Shyamsundar