title: MVC Modify List Within Object
body: <p>I have a basic Create / Edit / Delete / Index format created by MVC with my class (ClassA). The class contains a list of objects (ClassB).</p>
  
  <p>When I call the view to edit the object I can edit all the members of ClassA (textfields etc), but how can I edit the list of objects and then still have them returned back to the post action? </p>
  
  <p>When I try and create and iterate through the objects and add them to the form, it is not returned with the object (ClassA) and the list of objects is empty.</p>
  
  <p>Please help. What is the best practice for doing this?</p>
  
  <p>Below is an example </p>
  
  <pre><code> public ActionResult Edit(int id)
      {
          ClassA objA = db.ClassAs.Single(a =&gt; a.id == id);
  
          objA.myListOfBs.Add(new ClassB());
  
          return View(objA);
      }
  
       [HttpPost]
      public ActionResult Create(ObjectA obj)
      {
          obj.myListOfBs &lt;--- this is empty! :(
          if (ModelState.IsValid)
          {
          }
  
          return View(obj);
        }
  </code></pre>
  
  <p>Model classes are :</p>
  
  <pre><code>class ClassA {
          int id;
      List&lt;ClassB&gt; myListOfBs;
   }
  
   class ClassB {
      int id;
      string name;
   }
  </code></pre>
  
  <p>Here is my View</p>
  
  <pre><code>@model MyProject.Models.ClassA
  
  @{
      ViewBag.Title = "ClassA Object";
  }
  @using (Html.BeginForm()) {
      @Html.ValidationSummary(true)
  
      &lt;fieldset&gt;
          &lt;legend&gt;Objects&lt;/legend&gt;
  
          @Html.EditorFor(model =&gt; model.myListOfBs)
  
          &lt;p&gt;
              &lt;input type="submit" value="Save" /&gt;
          &lt;/p&gt;
      &lt;/fieldset&gt;
  }
  </code></pre>