title: How to create an input box (textbox) for a property of a model
body: <p>I have a simple shopping cart model which contains a list of items:</p>
  
  <pre><code>public class ShoppingCart
  {
      public List&lt;Item&gt; Items { get; set; }
      public double Tax { get; set; }
      // ... some other properties
  }
  
  public class Item
  {
      public string Name { get; set; }
      public int Quantity { get; set; }
      public double Price { get; set; }
      public double TotalCost { get { return Quantity * Price; } }
  }
  </code></pre>
  
  <p>I want to modify the quantity of a particular item and I've made the following View:</p>
  
  <pre><code>&lt;%using (Html.BeginForm("Recalculate", "ShoppingCart", Model))
    { %&gt;
  &lt;table id="cartTable" border ="5px" cellpadding="5px" cellspacing="5px" width="640"&gt;
      &lt;tr&gt;
          &lt;td&gt;&lt;b&gt;Item Name&lt;/b&gt;&lt;/td&gt;
          &lt;td&gt;&lt;b&gt;Item Qty&lt;/b&gt;&lt;/td&gt;
          &lt;td&gt;&lt;b&gt;Item Price&lt;/b&gt;&lt;/td&gt;
          &lt;td&gt;&lt;b&gt;Subtotal&lt;/b&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;%
      if (Model != null &amp;&amp; Model.Items != null)
      {
          foreach (ShoppingCart.Models.Item item in Model.Items)
          {
              %&gt;
              &lt;tr&gt;
                  &lt;td&gt;&lt;%: item.Name%&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;%: Html.TextBoxFor(m =&gt; m.Items[Model.Items.IndexOf(item)], new { @Value = item.Quantity })%&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;%: String.Format("{0:C}", item.Price)%&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;%: String.Format("{0:C}", item.TotalCost)%&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;%
          }
      } 
      %&gt;
      &lt;!-- some other rows/columns go here --&gt;
  &lt;/table&gt; 
  &lt;input type="submit" value="Update Cart" /&gt;
  &lt;%} %&gt;
  </code></pre>
  
  <p>And my controller: </p>
  
  <pre><code>public class ShoppingCartController : Controller
  {
  
      /// &lt;/summary&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      [HttpGet]
      public ActionResult Show(ShoppingCart model)
      {
          if (model!= null &amp;&amp; model.Items == null)
          {
              List&lt;Item&gt; items = new List&lt;Item&gt;();
              items.Add(new Item { Name = "Hat", Price = 20.0, Quantity = 1 });
              items.Add(new Item { Name = "Snowboard", Price = 430.0, Quantity = 1 });
              items.Add(new Item { Name = "Goggles", Price = 24.0, Quantity = 3 });
  
              model.Items = items;
              model.Tax = 6.5;
          }
          return View(model);
      }
  
      [HttpPost]
      public ActionResult Recalculate(ShoppingCart model)
      {
          if (model != null &amp;&amp; model.Items!=null)
          {
              foreach (Item item in model.Items)
              {
                  if (item.Quantity == 0)
                  {
                      model.Items.Remove(item);
                  }
                  else if (item.Quantity &lt; 0)
                  {
                      ModelState.AddModelError("error", "The quantity for " + item.Name + " must not be smaller than 0.");
                  }
              }
          }
          return RedirectToAction("Show", "ShoppingCart", model);
      }
  
  }
  </code></pre>
  
  <p>Unfortunately when I click on the Update Cart button it calls my Recalculate function, but now all of the items in the Items list are null. How can I keep the items AND update the quantity of a given item?</p>
  
  <p>In the BeginForm function I tried passing in the current model and not passing it a model at all... nothing changes. Could anybody help me figure this out?</p>