title: Need to pass a List<model> to the Http Post in a Controller
body: <p>I am passing a List to the view. In the view I need to pass back that list. It includes an editable check box, which are the items I really need. All of the other fields displayed are for read only purposes. If the user wants to designate the item as a clearance, then they check the isClearance checkbox. So when they hit the save button it hits the HttpPost Index(). However, it passes back a null. If I change it to be just a modelObject, not List, it works just fine. </p>
  
  <p>Here is the controller Index:</p>
  
  <pre><code> public ActionResult Index()
      {
          List&lt;ClearanceViewModel&gt; cc = new List&lt;ClearanceViewModel&gt;();
          ClearanceViewModel c = new ClearanceViewModel();
          c.sku = "123";
          c.title = "Test1";
          c.includeOnSite = true;
          c.productID = 123;
          c.salePrice = Convert.ToDecimal(2.99);
          c.RetailPrice = Convert.ToDecimal(4.99);
          c.isClearance = false;
          cc.Add(c);
  
          c.sku = "123";
          c.title = "Test1";
          c.includeOnSite = true;
          c.productID = 123;
          c.salePrice = Convert.ToDecimal(2.99);
          c.RetailPrice = Convert.ToDecimal(4.99);
          c.isClearance = false;
          cc.Add(c);
  
          return View(cc);
      }
  </code></pre>
  
  <p>Here is the View:</p>
  
  <pre><code>@model List&lt;PMS.Models.ClearanceViewModel&gt;
  @{    
  ViewBag.title = "Clearance List";    
  }
  &lt;fieldset&gt;
  &lt;legend&gt;&lt;span&gt;Clearance List&lt;/span&gt;&lt;/legend&gt;
  @using (Html.BeginForm())
  {
  if (Model.Count() &gt; 0)
  {    
      &lt;table class="list"&gt;
          &lt;tr&gt; 
              &lt;th&gt;Sku&lt;/th&gt;
              &lt;th&gt;Title&lt;/th&gt;
              &lt;th&gt;Include On Site&lt;/th&gt;
              &lt;th&gt;Sale price&lt;/th&gt;
              &lt;th&gt;Retail Price&lt;/th&gt;
              &lt;th&gt;Quantity&lt;/th&gt;
              &lt;th&gt;Clearance&lt;/th&gt;
          &lt;/tr&gt;
          @foreach(var item in Model)
          {
              @Html.HiddenFor(modelItem =&gt; item.productID);
              &lt;tr&gt;    
                  &lt;td&gt;@Html.DisplayFor(modelItem =&gt; item.sku)&lt;/td&gt;
                  &lt;td&gt;@Html.DisplayFor(modelItem =&gt; item.title)&lt;/td&gt;
                  &lt;td&gt;@Html.DisplayFor(modelItem =&gt; item.includeOnSite)&lt;/td&gt;
                  &lt;td&gt;@Html.DisplayFor(modelItem =&gt; item.salePrice)&lt;/td&gt;
                  &lt;td&gt;@Html.DisplayFor(modelItem =&gt; item.RetailPrice)&lt;/td&gt;
                  &lt;td&gt;@Html.DisplayFor(modelItem =&gt; item.quantity)&lt;/td&gt;
                  &lt;td&gt;@Html.EditorFor(modelItem =&gt; item.isClearance)&lt;/td&gt;        
              &lt;/tr&gt;
          }
      &lt;/table&gt;
     &lt;p&gt;
              &lt;input type="submit" value="Save" /&gt;
      &lt;/p&gt;
  
  }
  }
  &lt;/fieldset&gt;
  </code></pre>
  
  <p>Here is the HttpPost in the Controller (clearanceModel is null when it hits the foreach loop, when it should have a list of the items):</p>
  
  <pre><code>        [HttpPost]
      public ActionResult Index(List&lt;ClearanceViewModel&gt; clearanceModel)
      {
          foreach (ClearanceViewModel item in clearanceModel)
          {
              if (item.isClearance == true)
              {
                  // get the product object, so we can add it to the product Promotion of                      clearance
                  var product = _unitOfWork.ProductRepository.Get(filter: p =&gt; p.productID == item.productID).FirstOrDefault();
  
                  // Make sure that it isn't already in the product promotion for clearance
                  var productPromotion = _unitOfWork.ProductPromotionRepository.Get(filter: pp =&gt; pp.productID == product.productID &amp;&amp; pp.promotion.Name == "Clearance").FirstOrDefault();
  
                  //add the product promotion
                  if (productPromotion == null)
                  {
                      // get the clearance promotion
                      var promotion = _unitOfWork.PromotionRepository.Get(filter: pr =&gt; pr.Name == "Clearance").FirstOrDefault();
                      if (promotion != null)
                      {
                          ProductPromotion promo = new ProductPromotion();
                          promo.productID = product.productID;
                          promo.promotionID = promotion.promotionID;
                          promo.onDate = DateTime.Now;
                          promo.offDate = null;
                          promo.canOverwrite = true;
                          _unitOfWork.ProductPromotionRepository.Create(promo);
                          _unitOfWork.SaveChanges();
                      }
                  }
              }
          }
  </code></pre>