title: Asp.net mvc save One-to-many and passing master on submit
body: <p>I have a problem saving the detail of a master in asp.net mvc.</p>
  
  <p>For reference I am using nhibernate.</p>
  
  <p>I have a One-to-many relationship between the Store and Employee entities.
  I save the master and the detail in 2 steps.
  You create the Store first, save it, then you create the employees.</p>
  
  <p>Here are both classes:</p>
  
  <pre><code>public class Store
  {
      public Store()
      {
          Employees = new List&lt;Employee&gt;();
      }
  
      public virtual int Id { get; set; }
      public virtual string Name { get; set; }
      public virtual IList&lt;Employee&gt; Employees { get; set; }
  }
  
  public class Employee
  {
      public virtual int Id { get; set; }
      public virtual string Name { get; set; }
      public virtual Store Store { get; set; }
  }
  </code></pre>
  
  <p>So to create the Store I have the following code in the store's controller and view:</p>
  
  <pre><code>public virtual ActionResult Create()
  {
      var model = new StoreModel();
  
      return View(model);
  }
  
  [HttpPost]
  public ActionResult Create(StoreModel model)
  {
      if (ModelState.IsValid)
      {
          Store entity = new Store(model);
  
          Repository&lt;Store&gt; repository = new Repository&lt;Store&gt;();
          repository.Create(entity);
          return RedirectToAction("Index");
      }
  
      return View(model);
  }
  
  
  @model StoreModel
  @{
      ViewBag.Title = "Create store";
      Layout = "~/Views/Shared/_Layout.cshtml";
  }
  &lt;h2&gt;
      Create store
  &lt;/h2&gt;
  @Html.ValidationSummary(true)
  @using (Html.BeginForm())
  {
      &lt;div&gt;
          @Html.LabelFor(store =&gt; store.Name)
      &lt;/div&gt;
      &lt;div&gt;
          @Html.TextBoxFor(store =&gt; store.Name)
          @Html.ValidationMessageFor(store =&gt; store.Name)
      &lt;/div&gt;
      &lt;p&gt;
          &lt;input type="submit" value="Create"/&gt;
      &lt;/p&gt;
  }
  &lt;div&gt;
      @Html.ActionLink("Back", "Index")
  &lt;/div&gt;
  </code></pre>
  
  <p>That works fine, but my problem is when i try to save the employees, <strong>the store is always null</strong>.
  So to create the employees I have the following code in the employee's controller and view:</p>
  
  <pre><code>public virtual ActionResult Create(int storeId)
  {
      var model = new EmployeeModel();
      Repository&lt;Store&gt; repository = new Repository&lt;Store&gt;();
      model.Store = repository.Read(storeId);
  
      return View(model);
  }
  
  [HttpPost]
  public ActionResult Create(EmployeeModel model) //Problem here, store is null in the model
  {
      if (ModelState.IsValid)
      {
          Employee entity = new Employee(model);
  
          Repository&lt;Employee&gt; repository = new Repository&lt;Employee&gt;();
          repository.Create(entity);
          return RedirectToAction("Index");
      }
  
      return View(model);
  }
  
  @model EmployeeModel
  @{
      ViewBag.Title = "Create employee";
      Layout = "~/Views/Shared/_Layout.cshtml";
  }
  &lt;h2&gt;
      Create employee
  &lt;/h2&gt;
  @Html.ValidationSummary(true)
  @using (Html.BeginForm())
  {
      &lt;div&gt;
          @Html.LabelFor(employee =&gt; employee.Name)
      &lt;/div&gt;
      &lt;div&gt;
          @Html.TextBoxFor(employee =&gt; employee.Name)
          @Html.ValidationMessageFor(employee =&gt; employee.Name)
      &lt;/div&gt;
      &lt;p&gt;
          &lt;input type="submit" value="Create"/&gt;
      &lt;/p&gt;
  }
  &lt;div&gt;
      @Html.ActionLink("Back", "Index")
  &lt;/div&gt;
  </code></pre>
  
  <p>What am I doing wrong?</p>