title: MVC - Create object and related objects in one go
body: <p>I want to create a parent object with child/related objects in the same view.
  An example would be: create one Father (with some name) along with all his sons (with their names). I have created a view model:</p>
  
  <pre><code>public class FatherViewModel {
    public Father father {get; set;} // has 1 property Name
    public List&lt;Son&gt; {get; set;} // has 1 property Name
  }
  </code></pre>
  
  <p>My question is, how do I get the list of Sons back from the view when the post is performed?
  I have tried using HiddenFor for each Son id, but no matter what, the list is empty when returned to the controller.</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I tried the Editor Template example by Shyju described below, but my editor is never called.
  I have 1 object:</p>
  
  <pre><code>public class Person
  {
      public int Id { get; set; }
      public string Name { get; set; }
      public int? FatherId { get; set; }
      public virtual ICollection&lt;Person&gt; Children { get; set; }
  }
  </code></pre>
  
  <p>I did this: </p>
  
  <ol>
  <li>Scaffolded a full controller for Person with index, create, edit...</li>
  <li>Created EditorTemplates folder in Views->Person</li>
  <li><p>Created Person.cshtml: </p>
  
  <p><code>@model TestEditorTemplate.Models.Person
  &lt;div&gt;
      &lt;h4&gt;Child&lt;/h4&gt;
      @Html.TextBoxFor(s =&gt; s.Name)
      @Html.HiddenFor(s =&gt; s.Id)
  &lt;/div&gt;</code></p></li>
  <li><p>Added <code>@Html.EditorFor(m =&gt; m.Children)</code> to Create.cshtml</p></li>
  </ol>
  
  <p><strong>Questions:</strong></p>
  
  <ol>
  <li>How can <code>@Html.EditorFor(m =&gt; m.Children)</code>possibly work with the
  editor template when <code>m.Children</code> is a collection of <code>Person</code> and not a single
  <code>Person</code>?</li>
  <li>I want to create (not edit) a father including children at the same time. That means that I have no Ids to pass to the Create view to start with. How can this work? From the example by Shyju, the Ids are already created beforehand?? Or did I just misunderstand the example?</li>
  </ol>