title: ASP.NET MVC Session Based Model Binder - Update Value Back to Session
body: <p>Based on Steve Sanderson's sample in his <a href="http://books.google.ca/books?id=gzfFQrs_qQAC&amp;pg=PA224&amp;lpg=PA224&amp;dq=mvc%20session%20model%20binder&amp;source=bl&amp;ots=EXGajmo7TN&amp;sig=-TT0cCwtipPMN_FxnVqkhXbXW8A&amp;hl=en&amp;sa=X&amp;ei=KyLgUayzOOaziwK9goBY&amp;ved=0CHcQ6AEwBw#v=onepage&amp;q=mvc%20session%20model%20binder&amp;f=false" rel="nofollow">book</a>, He provided sample how to implement custom model binder by using session. of course it needs to register in global.asax.cs.</p>
  
  <pre><code>public class CartModelBinder : IModelBinder
  {
      private string sessionKey = "Cart";
  
      public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
      {
          Cart cart = (Cart)controllerContext.HttpContext.Session[this.sessionKey];
          if (cart == null)
          {
              cart = new Cart();
              controllerContext.HttpContext.Session[this.sessionKey] = cart;
          }
  
          return cart;
      }
  }
  </code></pre>
  
  <p>The mode class</p>
  
  <pre><code>public class Cart
  {
      private List&lt;int&gt; itemList;
  
      public List&lt;int&gt; ItemList
      {
          get
          {
              return this.itemList;
          }
      }
  
      public Cart()
      {
          this.itemList = new List&lt;int&gt;();
      }
  
      public void AddItem(int itemId)
      {
          this.itemList.Add(itemId);
      }
  }
  </code></pre>
  
  <p>Then in the controller/action to use it:</p>
  
  <pre><code>public class HomeController : Controller
  {
      public ActionResult Index(Cart cart)
      {
          return View();
      }
  
      [HttpPost]
      public ActionResult AddToCart(Cart cart, int id)
      {
          cart.AddItem(id);
  
          return View(cart);
      }
  }
  </code></pre>
  
  <p><code>Index.cshtml</code> view</p>
  
  <pre><code>@model MvcApplication2.Models.Cart
  
  @{
      Layout = null;
  }
  
  &lt;!DOCTYPE html&gt;
  
  &lt;html&gt;
  &lt;head&gt;
      &lt;meta name="viewport" content="width=device-width" /&gt;
      &lt;title&gt;Index&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;div&gt;
          @using (@Html.BeginForm("AddToCart", "Home")) {
          &lt;h1&gt;Cart form&lt;/h1&gt;
              @Html.TextBox("id")
  
              &lt;input type="submit" /&gt;
          }
      &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><code>AddToCart.cshtml</code> view</p>
  
  <pre><code>@model MvcApplication2.Models.Cart
  
  @{
      Layout = null;
  }
  
  &lt;!DOCTYPE html&gt;
  
  &lt;html&gt;
  &lt;head&gt;
      &lt;meta name="viewport" content="width=device-width" /&gt;
      &lt;title&gt;AddToCart&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;div&gt;
          @foreach (int itemId in Model.ItemList)
          { 
              @Html.Label(itemId.ToString())&lt;br /&gt;
          }
          @Html.ActionLink("Go back", "Index")
      &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>But my question here is I don't see any code to update value back to session. How does it happen?</p>