title: Update a Model and a Collection of Models on a Single View (MVC4)
body: <p>I've been working on an MVC 4 Application and have run into a problem when attempting to update Models in a <code>ViewModel</code>.  </p>
  
  <p>My <code>ViewModel</code> (detailed below) contains one <code>ComplexObjectOne</code> and a <code>List&lt;ComplexObjectTwo&gt;</code>. </p>
  
  <p>My <strong>GET</strong> <code>ActionResult</code> successfully populates the <code>ViewModel</code> from a database and everything displays correctly on my <code>View</code>. </p>
  
  <p>The problem is encountered when attempting to pass the <code>ComplexObjectOne</code> and <code>List&lt;ComplexObjectTwo&gt;</code> to the <strong>POST</strong> <code>ActionResult</code>. </p>
  
  <p>The <code>ComplexObject</code> is passed correctly but everything I've tried fails pass the <code>List&lt;ComplexObjectTwo&gt;</code> collection.</p>
  
  <p>My ComplexModelOne <code>Model</code></p>
  
  <pre class="lang-cs prettyprint-override"><code>public class Test
  {
      public int Id {get;set;}
      public string Result {get;set;}
  
      public virtual ICollection&lt;TestResult&gt; TestResults {get;set;}
  }
  </code></pre>
  
  <p>My ComplexModelTwo <code>Model</code></p>
  
  <pre class="lang-cs prettyprint-override"><code>public class TestResult
  {
      public int Id {get;set;}
      public string Result {get;set;}
      public string Comment {get;set;}
  
      public virtual Test Test{get;set;}
  }
  </code></pre>
  
  <p>My <code>ViewModel</code></p>
  
  <pre class="lang-cs prettyprint-override"><code>public class TestingViewModel
  {
      public TestingViewModel()
      {
          if(TestResults == null)
          {
              TestResults = new List&lt;TestResult&gt;();
          }
      }
  
      public Test Test {get;set;}
      public IEnumerable&lt;TestResult&gt; TestResults {get;set;}
  }
  </code></pre>
  
  <p>My Edit() <strong>GET</strong> <code>ActionResult</code></p>
  
  <pre class="lang-cs prettyprint-override"><code>public ActionResult Edit(int id = 0)
      {
          var viewModel = new TestingViewModel();
  
          Test test = testRepo.GetTestById(id);
          var results = test.TestResults;
  
          viewModel.Test = test;
          viewModel.TestResults = results;
          return View(viewModel);
      }
  </code></pre>
  
  <p>My Edit() <strong>POST</strong> <code>ActionResult</code></p>
  
  <pre class="lang-cs prettyprint-override"><code>[HttpPost]
  [ValidateAntiForgeryToken]
  public ActionResult Edit(TestingViewModel model)
  {
      // do update - left out for brevity
  }
  </code></pre>
  
  <p>My Edit.cshtml <code>View</code></p>
  
  <pre class="lang-cs prettyprint-override"><code>@model Namespace.Models.ViewModels.TestingViewModel
  
  @{
      ViewBag.Title = "Edit";
  }
  
  &lt;h2&gt;Edit&lt;/h2&gt;
  
  @using (Html.BeginForm())
  {
      @Html.AntiForgeryToken()
      @Html.ValidationSummary(true)
  
  
      @Html.EditorFor(model =&gt; model.Test, "TestHeader")
  
      &lt;table&gt;
          &lt;tr&gt;
              &lt;th&gt;Test&lt;/th&gt;
              &lt;th&gt;Result&lt;/th&gt;
              &lt;th&gt;Comment&lt;/th&gt;
          &lt;/tr&gt;
          @Html.EditorFor(model =&gt; model.TestResults, "TestResults")
  
      &lt;/table&gt;
  
      &lt;input type="submit" value="Update"/&gt;
  }
  </code></pre>
  
  <p>Within my <code>View</code> I do use a couple of <code>EditorTemplates</code> to display the property fields. </p>
  
  <p>Any assistance, comments, or suggestions will be much appreciated. I'd like to be able to accomplish updating these entities on a single page instead of multiple pages which I resorted to in the Create() steps.</p>
  
  <p>Thank you,</p>
  
  <p>Patrick H. (stpatrck)</p>