title: ASP MVC3 view model losing data from collection objects on post back to edit method
body: <p>My ASP MVC3 view model works just fine with the Edit method loads this particular page, however when the user hits save and does a postback, all information contained in the two collection objects (<code>List&lt;T&gt;</code>) is lost. Any ideas? </p>
  
  <p>Here is the code from the view. This loads all agent information (ID and state code) properly</p>
  
  <pre><code>    @for (int i = 0; i &lt; Model.Fixed.Count; i++)
      {           
  
          if(!String.IsNullOrWhiteSpace(Model.Fixed[i].AgentId))
          {
             fixedRow++;
  
             if (fixedRow &gt; 2)
             {
                 var rowId = "row" + fixedRow.ToString() + "F";
                 &lt;tr id=rowId class="noSee"&gt;
                      &lt;td&gt;
                          @Html.DropDownListFor(model =&gt; model.Fixed[i].StateCode,
                           (SelectList)ViewBag.StateCodeList, Model.Fixed[i].StateCode)
                      &lt;/td&gt;
                      &lt;td&gt;
                          @Html.EditorFor(model =&gt; model.Fixed[i].AgentId)
                          @Html.ValidationMessageFor(model =&gt; model.Fixed[i].AgentId)
                      &lt;/td&gt;
                      @if(fixedRow &gt; 1)
                      {
                          var send = "MoreFixed(" + (fixedRow + 1).ToString() + ");";
                          var dataId = "plus" + fixedRow.ToString() + "F;";
                          &lt;td id=@dataId  class="more" onclick=@send&gt;+&lt;/td&gt;
                      }
                 &lt;/tr&gt; 
             }
             else
             {
                  &lt;tr&gt;
                      &lt;td&gt;
                          @Html.DropDownListFor(model =&gt; model.Fixed[i].StateCode,
                           (SelectList)ViewBag.StateCodeList, Model.Fixed[i].StateCode)
                      &lt;/td&gt;
                      &lt;td&gt;
                          @Html.EditorFor(model =&gt; model.Fixed[i].AgentId)
                          @Html.ValidationMessageFor(model =&gt; model.Fixed[i].AgentId)
                      &lt;/td&gt;
                      @if(fixedRow &gt; 1)
                      {
                          var send = "MoreFixed(" + (fixedRow + 1).ToString() + ");";
                          var id = "plus" + fixedRow.ToString() + "F;";
                          &lt;td id=@id class="more" onclick=@send&gt;+&lt;/td&gt;
                      }
  
                  &lt;/tr&gt; 
             }        
          }
      }
  </code></pre>
  
  <p>Here is the code from the view model</p>
  
  <pre><code>public class BankListViewModel
  {
      public int ID { get; set; }
      public string BankName { get; set; }
      public string Tier { get; set; }
      public string SpecialNotes { get; set; }
      public string WelcomLetterReq { get; set; }
  
      public List&lt;BankListAgentId&gt; Fixed { get; set; }
      public List&lt;BankListAgentId&gt; Variable { get; set; }
      public List&lt;BankListAttachments&gt; Attachments { get; set; }
  
      public BankListViewModel()
      {
          //Initialize Fixed and Variable stat Lists
          Fixed = new List&lt;BankListAgentId&gt;();
          Variable = new List&lt;BankListAgentId&gt;();
  
          Models.BankListAgentId agentId = new BankListAgentId();
  
          for (int i = 0; i &lt; 5; i++)
          {
              Fixed.Add(agentId);
              Variable.Add(agentId);                
          }
  
          //Initialize attachment Lists
          Attachments = new List&lt;BankListAttachments&gt;();
          Attachments.Add(new BankListAttachments());
      }
  }
  </code></pre>
  
  <p>This is the controller method that receives the post back. Again, when the page loads, the 
  <code>GET</code> Edit method collects all the proper information from the database and returns the view model to the view corretly.</p>
  
  <pre><code>    [HttpPost]
      public ActionResult Edit(BankListViewModel viewModel)
      {
          if (ModelState.IsValid)
          {
              List&lt;BankListAgentId&gt; agentId = new List&lt;BankListAgentId&gt;();
              List&lt;BankListAttachments&gt; attachments = new List&lt;BankListAttachments&gt;();
              BankListMaster master = new BankListMaster();
  
              master = decipherViewModel(viewModel, out agentId, out attachments);
  
              db.Entry(master).State = EntityState.Modified;
              db.SaveChanges();
  
  
              return RedirectToAction("Index");
          }
          ViewBag.ID = new SelectList(db.BankListAgentId, "ID", "FixedOrVariable", viewModel.ID);
          return View(viewModel);
      } 
  
      private BankListMaster decipherViewModel(BankListViewModel viewModel, out List&lt;BankListAgentId&gt; agentId, out List&lt;BankListAttachments&gt; attachments)    
      {
          //Initialize
          BankListMaster banklistmaster = new BankListMaster();
          agentId = new List&lt;BankListAgentId&gt;();
          attachments = new List&lt;BankListAttachments&gt;();
  
          viewModel.BankName = banklistmaster.BankName;
          viewModel.SpecialNotes = banklistmaster.SpecialNotes;
          viewModel.Tier = banklistmaster.Tier;
  
          foreach (var item in viewModel.Fixed)
          {
              item.ID = viewModel.ID;
              agentId.Add(item);
          }
  
          foreach (var item in viewModel.Variable)
          {
              item.ID = viewModel.ID;
              agentId.Add(item); 
          }
  
          foreach (var item in viewModel.Attachments)
          {
              item.ID = viewModel.ID;
              attachments.Add(item);
          }
  
          return banklistmaster;
      }
  </code></pre>