title: MVC Enum Model Binding in For Loop
body: <p>I have an MVC 5 app where I am using a for loop so I can bind a collection when passing back to the controller.  This works fine for all my properties except for the one that is based on a DropDownFor type.</p>
  
  <p>The problem is the name of the property is not getting set to "product.[0].TypeOfSubscription.</p>
  
  <p>I have tried 3 different ways:  The first 2 method end up with a name of [0].TypeOfSubscription and the 3rd one does have the correct name product[0].TypeOfSubscription but there is no binding occuring when I pass it back to the controller.</p>
  
  <p>I think the problem is that the 3rd option is binding but because it is hidden it is not getting the selected value assigned.</p>
  
  <pre><code>@Html.EnumDropDownListFor(modelItem =&gt; Model[i].TypeOfSubscription)
  
  @Html.EnumDropDownListFor(modelItem =&gt; Model[i].TypeOfSubscription, 
                                              new { name = "product[" + @i + "].TypeOfSubscription"})
  
  @Html.Hidden("product[" + @i + "].TypeOfSubscription",
                                              Model[i].TypeOfSubscription)
  </code></pre>
  
  <p>Model</p>
  
  <pre><code>public class VmStoreProducts
      {
          public VmStoreProducts()
          {
              NoOfUsers = 1;
          }
  
          public enum SubscriptionType
          {
              Monthly,
              Annual
  
          }
          public int MojitoProductId { get; set; }
          [Display(Name = "Category")]
          public string ProductCategory { get; set; }
          public virtual string Name { get; set; }
          public string Description { get; set; }
          [Display(Name = "Image")]
          public byte[] ImageData { get; set; }
          [Display(Name = "Type of Subscription")]
          public SubscriptionType TypeOfSubscription { get; set; }
          public decimal Price { get; set; }
          [Display(Name = "No. of Users")]
          public int NoOfUsers { get; set; }
  
          [Display(Name = "Total Price")]
          [DisplayFormat(DataFormatString = "{0:C}")]
          public decimal TotalPrice { get; set; }
  
  
      }
  </code></pre>
  
  <p>For Loop - View</p>
  
  <pre><code>@model PagedList.IPagedList&lt;VmStoreProducts&gt;
  @using Mojito.Domain
  @using PagedList.Mvc;
  &lt;link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" /&gt;
  @{
      ViewBag.Title = "Index";
      Layout = "~/Views/Shared/_Layout.cshtml";
  }
  
  &lt;h2&gt;Mojito Products&lt;/h2&gt;
  &lt;div class="col-md-9"&gt;&lt;/div&gt;
  &lt;div class="col-md-3"&gt;
      @using (Html.BeginForm("Index", "MojitoProducts", FormMethod.Get))
      {
          &lt;p&gt;
              @Html.TextBox("SearchString", ViewBag.CurrentFilter as string)
              &lt;input type="submit" value="Search" /&gt;
          &lt;/p&gt;
      }
  &lt;/div&gt;
  @using (Html.BeginForm("AddToCart", "ShoppingCart", FormMethod.Post))
  {
      &lt;table class="table"&gt;
          &lt;tr&gt;
              &lt;th&gt;
                  @Html.DisplayNameFor(model =&gt; model.FirstOrDefault().ImageData)
              &lt;/th&gt;
              &lt;th&gt;
                  @Html.ActionLink("Category", "Index", new { sortOrder = ViewBag.SortByCategory, currentFilter = ViewBag.CurrentFilter })
              &lt;/th&gt;
              &lt;th&gt;
                  @Html.ActionLink("Product", "Index", new { sortOrder = ViewBag.SortByProduct, currentFilter = ViewBag.CurrentFilter })
              &lt;/th&gt;
              &lt;th&gt;
                  @Html.DisplayNameFor(model =&gt; model.FirstOrDefault().Description)
              &lt;/th&gt;
              &lt;th&gt;
                  @Html.DisplayNameFor(model =&gt; model.FirstOrDefault().TypeOfSubscription)
              &lt;/th&gt;
              &lt;th&gt;
                  @Html.ActionLink("Price", "Index", new { sortOrder = ViewBag.SortByPrice, currentFilter = ViewBag.CurrentFilter })
              &lt;/th&gt;
              &lt;th&gt;
                  @Html.DisplayNameFor(model =&gt; model.FirstOrDefault().NoOfUsers)
              &lt;/th&gt;
              &lt;th&gt;
                  @Html.DisplayNameFor(model =&gt; model.FirstOrDefault().TotalPrice)
              &lt;/th&gt;
  
              &lt;th&gt;&lt;/th&gt;
          &lt;/tr&gt;
  
          @for (int i = 0; i &lt; Model.Count; i++)
          {
  
              &lt;tr&gt;
                  &lt;td&gt;
                      @if (Model[i].ImageData != null)
                      {
                          &lt;div class="pull-left" style="margin-right: 10px"&gt;
                              &lt;img class="img-thumbnail" width="75" height="75"
                                   src="@Url.Action("GetImage", "MojitoProducts",
                                            new { Model[i].MojitoProductId })" /&gt;
                          &lt;/div&gt;
                      }
                  &lt;/td&gt;
                  &lt;td&gt;
                      @Html.DisplayFor(modelItem =&gt; Model[i].ProductCategory)
                  &lt;/td&gt;
                  &lt;td&gt;
                      @Html.TextBox("product[" + @i + "].Name",
                                  Model[i].Name, new { @readonly = "readonly" })
                  &lt;/td&gt;
                  &lt;td&gt;
                      @Html.DisplayFor(modelItem =&gt; Model[i].Description)
                  &lt;/td&gt;
                  &lt;td&gt;
                      @Html.EnumDropDownListFor(modelItem =&gt; Model[i].TypeOfSubscription)
  
                      @Html.EnumDropDownListFor(modelItem =&gt; Model[i].TypeOfSubscription, 
                                              new { name = "product[" + @i + "].TypeOfSubscription"})
  
                      @Html.TextBox("product[" + @i + "].TypeOfSubscription",
                                           Model[i].TypeOfSubscription, new { hidden=true })
                  &lt;/td&gt;
                  &lt;td&gt;
                      @Html.TextBox("product[" + @i + "].Price",
                          Model[i].Price, new { @readonly = "readonly", style = "width:50px" })
                  &lt;/td&gt;
                  &lt;td&gt;
                      @Html.TextBox("product[" + @i + "].NoOfUsers",
                          Model[i].NoOfUsers, new { type = "number", min = "0", style = "width:50px" })
                  &lt;/td&gt;
                  &lt;td&gt;
                      @Html.TextBox("product[" + @i + "].TotalPrice",
                          Model[i].TotalPrice, new { style = "width:50px" })
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;div class="pull-right"&gt;
                          @if (Request.Url != null)
                          {
                              @Html.Hidden("product[" + @i + "].MojitoProductId",
                                        Model[i].MojitoProductId)
                              @Html.Hidden("returnUrl", Request.Url.PathAndQuery)
                          }
  
                      &lt;/div&gt;
  
                  &lt;/td&gt;
              &lt;/tr&gt;
          }
          &lt;tr&gt;
              &lt;td colspan="6"&gt;
                  &lt;div class="pull-right"&gt;
                      &lt;input type="submit" class="btn btn-success" value="Add to cart" /&gt;
                  &lt;/div&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
  
  
  
      &lt;/table&gt;
  
  }
  </code></pre>
  
  <p>Controller Method</p>
  
  <pre><code>public ActionResult AddToCart(List&lt;VmStoreProducts&gt; product, string returnUrl)
          {
              ShoppingCart cartObjects = (Session["CartObjects"] as ShoppingCart) ?? new ShoppingCart();
              Session["CartObjects"] = cartObjects;
  
              foreach (var item in product)
              {
                  if (item.NoOfUsers &gt; 0)
                  {
                      cartObjects.AddItem(item);
                  }
  
              }
  
              return RedirectToAction("Index", new { returnUrl });
          }
  </code></pre>