2

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;}

1.) Scenario 1: The main view has something like the following

@model MasterDetailSample.Models.HomeViewModel

@using (Html.BeginForm()) {
    <div>
        @{Html.RenderPartial("_PersonView", @Model);}
    </div>
    <input type="submit" value="Save" />        
}

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))

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

This seems to work however notice the following variation scenario 2 (where I only pass to the partial the Employee object)

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.

So from the parent form i have

@{Html.RenderPartial("_MasterView", @Model.Employee);}

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

i.e.

    [HttpPost]
    public ActionResult Index(ModelViewModel model) {
        **//model.Employee is null!**
        return View();
    }

Why? (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

@model MasterDetailSample.Models.Person

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.

Can this be done? If yes how?

thx

MrMVCMan
  • 490
  • 2
  • 7
  • 20

1 Answers1

1

You have a couple of options:

1) One I recommend -> Dont use partial views, instead use EditorFor and create an editor template for Person. With partial views the context is whatever model you pass into the view, this is why your example (1) works and (2) not. However with editor templates the parent context is taken into consideration with the html helpers and will generate the correct input names. Have a look at Darin Dimitrov's answer to a similar question.

2) Use your second example as is, but change the post action to look something like this:

[HttpPost]
public ActionResult Index(ModelViewModel model) {
    TryUpdateModel(model.Employee);
    **//model.Employee should now be filled!**
    return View();
}

3) Use custom html helpers that accepts prefix for input, see this answer I posted a while back for example code. You could then use this inside your partial view.

Community
  • 1
  • 1
Beyers
  • 8,968
  • 43
  • 56