title: ASP.NET MVC 3 - EF 4.2 code-first - master-detail editing
body: <p>I'm stumped with a problem in ASP.NET MVC 3 - handling editing and updating of a master-detail relationship from a single MVC view.</p>
  
  <p>I'm using Entity Framework 4.2 code-first to build a small app. It uses a <code>Person</code> class (which has <code>Name</code>, <code>FirstName</code> and so on), and an <code>Address</code> class (with <code>Street</code>, <code>Zipcode</code>, <code>City</code> etc.).</p>
  
  <p>These two classes have a m:n relationship to one another, modelled in EF code-first with:</p>
  
  <pre><code>public class Person
  {
      private List&lt;Address&gt; _addresses;
  
      // bunch of scalar properties like ID, name, firstname
  
      public virtual List&lt;Address&gt; Addresses
      {
          get { return _addresses; }
          set { _addresses = value; }
      }
  }
  
  public class Address
  {
      private List&lt;Person&gt; _people;
  
      // bunch of scalar properties like AddressID, street, zip, city
  
      public virtual List&lt;Person&gt; People
      {
          get { return _people; }
          set { _people = value; }
      }
  }
  </code></pre>
  
  <p>I filled the database manually with some data and retrieval of the data using EF 4.2 works just fine.</p>
  
  <p>I have an ASP.NET MVC <code>PersonController</code>, in which I am loading a <code>Person</code> including all its current addresses, and show it using a Razor view. I'd like to allow the user to change or update existing addresses, as well as delete or insert addresses. The controller gets the data for that person from a WCF service like this:</p>
  
  <pre><code>public ActionResult Edit(int id)
  {
      Person person = _service.GetPerson(id);
      return View(person);
  }
  </code></pre>
  
  <p>and the person's addresses are filled properly when retrieving.</p>
  
  <p>The view for this works fine - it shows all the data from the <code>Person</code> instance as expected. </p>
  
  <pre><code>@model DomainModel.Person
  
  &lt;h2&gt;Edit&lt;/h2&gt;
  
  @using (Html.BeginForm()) {
      @Html.ValidationSummary(true)
      &lt;fieldset&gt;
          &lt;legend&gt;Person&lt;/legend&gt;
  
          @Html.HiddenFor(model =&gt; model.PersonId)
  
          &lt;div class="editor-label"&gt;
              @Html.LabelFor(model =&gt; model.Name)
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.EditorFor(model =&gt; model.Name)
              @Html.ValidationMessageFor(model =&gt; model.Name)
          &lt;/div&gt;
  
          .... and a few more of those scalar properties being edited....  
  
          &lt;hr/&gt;
          &lt;h4&gt;Addresses&lt;/h4&gt;
  
          @foreach (var item in Model.Addresses)
          {
              &lt;tr&gt;
                  &lt;td&gt;
                      @Html.EditorFor(modelItem =&gt; item.Street)
                  &lt;/td&gt;
                  &lt;td&gt;
                      @Html.EditorFor(modelItem =&gt; item.ZipCode)
                  &lt;/td&gt;
                  &lt;td&gt;
                      @Html.EditorFor(modelItem =&gt; item.City)
                  &lt;/td&gt;
              &lt;/tr&gt;
          }
          &lt;hr/&gt;
          &lt;p&gt;
              &lt;input type="submit" value="Save" /&gt;
          &lt;/p&gt;
      &lt;/fieldset&gt;
  }
  </code></pre>
  
  <p>However, when I'm trying to handle the POST upon saving the person being edited, I cannot seem to get access to the addresses that were displayed (and possibly edited) in the view:</p>
  
  <pre><code>    [HttpPost]
      public ActionResult Edit(Person person)
      {
          if (ModelState.IsValid)
          {
              _service.UpdatePerson(person);
              return RedirectToAction("Index");
          }
          return View(person);
      } 
  </code></pre>
  
  <p>The <code>person.Addresses</code> property is always null in this case. Hmmm.... what am I missing?? How can I edit a Person-to-Addresses relationship on an ASP.NET MVC view, and get back the <code>Person</code> <em>AND</em> its associated <code>Addresses</code> from my view, so I can pass them to EF to save them back to the database tables?? </p>