title: MVC 3 Partial View and what I need to send to it?
body: <p>I am wondering about a couple variations of forms and partial forms.  The submit is on the parent page and I have varied what I pass to the partial view.  I have a parent view with a related HomeViewModel (which has public properties Name and public Person Employee {get;set;}</p>
  
  <p>1.) Scenario 1: The main view has something like the following</p>
  
  <pre><code>@model MasterDetailSample.Models.HomeViewModel
  
  @using (Html.BeginForm()) {
      &lt;div&gt;
          @{Html.RenderPartial("_PersonView", @Model);}
      &lt;/div&gt;
      &lt;input type="submit" value="Save" /&gt;        
  }
  </code></pre>
  
  <p>In this scenario I am passing to the partial view _PersonView the entire HomeViewModel.  Within _PersonView partial view I have to reference a property of the HomeViewModel i.e. Person object via @model.Employee.Name (in this scenario the submit is on the parent form (not within the partial view))</p>
  
  <p>When I hit submit on the form (POST) in the controller i have to access the property of Employee "Name" via the following model.Employee.Name</p>
  
  <p>This seems to work however notice the following variation scenario 2 (where I only pass to the partial the Employee object)</p>
  
  <p>2.) Scenario 2
  In this scenario I only want to send the Employee object to the partial view.  Again the begin form and submit is on the parent form.</p>
  
  <p>So from the parent form i have </p>
  
  <pre><code>@{Html.RenderPartial("_MasterView", @Model.Employee);}
  </code></pre>
  
  <p>and so within the partial view i reference the Name property of the Person object via @Employee.Name  Now when I submit the form within the controller the Employee object is not available from the auto model binder.  I can access the properties via formcollection but not from the model parameter</p>
  
  <p>i.e.</p>
  
  <pre><code>    [HttpPost]
      public ActionResult Index(ModelViewModel model) {
          **//model.Employee is null!**
          return View();
      }
  </code></pre>
  
  <p><strong>Why</strong>? (is model.Employee null)  I would like my partial view to only accept an object of type Person however after submitting from the parent page, the Employee property is null.  On the partial view I am using the following on the @model line</p>
  
  <pre><code>@model MasterDetailSample.Models.Person
  </code></pre>
  
  <p>I would like the partial to only need a Person object to be sent to it, but I would like the submit on the main form.  If i do it this way I can re-use the partial view in a few situations however IF i must send HomeViewModel i have significantly limited how I can use this partial view.  So, again, I only want to use Person as the model with the partial view but I need to be able to access the properties when submitted from the parent view.</p>
  
  <p>Can this be done? If yes how?</p>
  
  <p>thx</p>