title: Updating multiple records at the same time in MVC
body: <p>Trying to update more than 1 record using a form submission in MVC (bringing existing data into the View from the DB and allowing user to make changes).</p>
  
  <p>In the View, I can return as many existing records as I need ready to be edited, without a problem: </p>
  
  <pre><code>@model IEnumerable&lt;object&gt;
  @{
      List&lt;Portal.Models.Primary_Client&gt; PClient = Model.ToList()[0] as List&lt;Portal.Models.Primary_Client&gt;;
      List&lt;Portal.Models.Primary_Dependent&gt; PDependant = Model.ToList()[1] as List&lt;Portal.Models.Primary_Dependent&gt;;
      List&lt;Portal.Models.Primary_AnnExp&gt; PAnnExp = Model.ToList()[2] as List&lt;Portal.Models.Primary_AnnExp&gt;;
      List&lt;Portal.Models.Primary_IncExp&gt; PIncEx = Model.ToList()[3] as List&lt;Portal.Models.Primary_IncExp&gt;;
      List&lt;Portal.Models.Primary_InvAssets&gt; PInvAssets = Model.ToList()[4] as List&lt;Portal.Models.Primary_InvAssets&gt;;
      List&lt;Portal.Models.Primary_Liabilities&gt; PLiabilities = Model.ToList()[5] as List&lt;Portal.Models.Primary_Liabilities&gt;;
      List&lt;Portal.Models.Primary_LifeAssets&gt; PLAssets = Model.ToList()[6] as List&lt;Portal.Models.Primary_LifeAssets&gt;;
  
      ViewBag.Title = "Completed Form";
      Layout = "~/Views/Shared/_Layout.cshtml";
  
  }
  
                      @using (Html.BeginForm("EditPLA/" + newid, "Forms", FormMethod.Post))
                  {
  
                      for (int i = 0; i &lt; PLAssets.Count; i++)
                      {
  
                          &lt;div class="form-group"&gt;
                              @Html.HiddenFor(Model =&gt; PLAssets[i].LifAsset_ID)
                              @Html.HiddenFor(Model =&gt; PLAssets[i].PLForm_ID)
                              @Html.HiddenFor(Model =&gt; PLAssets[i].Client_ID_PriFK)
                          &lt;/div&gt;
                          &lt;div class="container"&gt;
                              &lt;div class="row"&gt;
                                  &lt;div class="col-sm"&gt;
                                      @Html.Label("Asset Type")
                                      @Html.EditorFor(Model =&gt; PLAssets[i].PLAsset_Type, new { htmlAttributes = new { @class = "form-control" } })
                                  &lt;/div&gt;
                                  &lt;div class="col-sm"&gt;
                                      @Html.Label("Asset Amount")
                                      @Html.EditorFor(Model =&gt; PLAssets[i].PLAmount_Per_Annum, new { htmlAttributes = new { @class = "form-control" } })
                                  &lt;/div&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                          &lt;br /&gt;
                      }
  
                      &lt;div class="button" style="text-align:center;"&gt;
                          &lt;input type="submit" class="btn btn-success" value="Save" /&gt;
                          &lt;button class="btn btn-danger" data-dismiss="modal"&gt;Cancel&lt;/button&gt;
  
                      &lt;/div&gt;
                  }
  </code></pre>
  
  <p>However, my model does not populate properly when I submit the form as the field names are not passing as expected and do not match my model field names. I suspect this is because of how I create the fields using the loop.</p>
  
  <p>My Editor field names look like this: </p>
  
  <pre><code>&lt;input class="form-control text-box single-line" id="CS___8__locals1_PLAssets_0__PLAmount_Per_Annum" name="CS$&lt;&gt;8__locals1.PLAssets[0].PLAmount_Per_Annum" type="text" value="3"&gt;
  </code></pre>
  
  <p>When I would have expected to see something more like this:</p>
  
  <pre><code>&lt;input class="form-control text-box single-line" id="PLAmount_Per_Annum" name="PLAmount_Per_Annum" type="text" value="3"&gt;
  </code></pre>
  
  <p>I believe this is made more difficult than usual because I have several Models being passed to my View through an Object, but I don't know how else to create these fields with the correct name.</p>
  
  <p>FYI - My Controller looks like this:</p>
  
  <pre><code>     [HttpPost]
          public ActionResult EditPLA(List&lt;Primary_LifeAssets&gt; Assets, int id)
          {
              if (ModelState.IsValid)
              {
                  foreach (var i in Assets)
                  {
                      var c = db1.Primary_LifeAssets.Where(a =&gt; a.LifAsset_ID.Equals(i.LifAsset_ID)).FirstOrDefault();
                      if (c != null)
                      {
                          c.PLForm_ID = i.PLForm_ID;
                          c.PLAmount_Per_Annum = i.PLAmount_Per_Annum;
                          c.PLAsset_Type = i.PLAsset_Type;
                      }
                  }
                  db1.Entry(Assets).State = EntityState.Modified;
                  db1.SaveChanges();
                  ViewBag.Message = "Successfully Updated.";
                  return RedirectToAction("Existing/" + id, "Forms");
              }
  
              ViewBag.Message = "Save failed.";
              return RedirectToAction("Existing/" + id, "Forms");
          }
  </code></pre>