title: What can cause the Model Binding to fail in ASP.NET MVC 1.0?
body: <p>Here is my class...any pointers?</p>
  
  <pre><code>public class Cart
  {
      private List&lt;CartLine&gt; lines = new List&lt;CartLine&gt;();
      public IList&lt;CartLine&gt; Lines { get { return lines.AsReadOnly(); } }
  
      public void AddItem(Product product, int quantity)
      {
          var line = lines.FirstOrDefault(l =&gt; l.Product.ProductID == product.ProductID);
  
          if (line == null)
              lines.Add(new CartLine { Product = product, Quantity = quantity });
          else
              line.Quantity += quantity;
  
      }
  
      public decimal ComputeTotalValue() 
      {
          return lines.Sum(l =&gt; l.Product.Price * l.Quantity);
      }
  
      public void Clear() 
      {
          lines.Clear();
      }
  
      public void RemoveLine(Product product)
      {
          lines.RemoveAll(l =&gt; l.Product.ProductID == product.ProductID);
      }
  }
  
  public class CartLine
  {
      public Product Product { get; set;}
      public int Quantity { get; set;}
  }
  </code></pre>
  
  <p>Adding the Action method in the controller.</p>
  
  <pre><code>public RedirectToRouteResult AddToCart(Cart cart, int productID, string returnUrl)
          {
              Product product = productsRepository.Products.FirstOrDefault(p =&gt; p.ProductID == productID);
              cart.AddItem(product, 1);
              return RedirectToAction("Index", new { returnUrl });
          }
  
          public RedirectToRouteResult RemoveFromCart(Cart cart, int productID, string returnUrl)
          {
              Product product = productsRepository.Products.FirstOrDefault(p =&gt; p.ProductID == productID);
              cart.RemoveLine(product);
              return RedirectToAction("Index", new { returnUrl });
          }
  
          public ViewResult Index(Cart cart, string returnUrl)
          {
              ViewData["returnUrl"] = returnUrl;
              ViewData["CurrentCategory"] = "Cart";
              return View(cart);
          }
  </code></pre>
  
  <p>My custom model binder:</p>
  
  <pre><code>public class CartModelBinder : IModelBinder
      {
          private const string cartSessionKey = "_cart";
  
          #region IModelBinder Members
  
          public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
          {
              if (bindingContext.Model != null)
                  throw new InvalidOperationException("Cannot update instances");
              Cart cart = (Cart)controllerContext.HttpContext.Session[cartSessionKey];
              if (cart == null)
              {
                  cart = new Cart();
                  controllerContext.HttpContext.Session["cartSessionKey"] = cart;
              }
              return cart;
          }
          #endregion
      }
  </code></pre>