I am programming in C # ASP.NET MVC4 (Razor engine). I need to create a partial view and reuse it in several places. The problem is that the view is a form, and in some cases I will need to use it with the ViewModel. My question is how model binding will work because in ViewModel this is a property of a property. Example:
public class PersonModel { public string FirstName { get; set; } public string LastName { get; set; } } public class OfficeViewModel { public PersonModel Person { get; set; }
A partial view for PersonModel will be:
@model SomeNameSpace.PersonModel @Html.TextBoxFor(m => m.FirstName) @Html.TextBoxFor(m => m.LastName)
When this view is displayed, it will look like this:
<input type="textbox" id="FirstName" name="FirstName" /> <input type="textbox" id="LastName" name="LastName" />
Now I want to use the same view with my OfficeViewModel. In this case, I would do this in my Office view:
@{ Html.RenderPartial("Person", Model.Person); }
When this partial view is displayed, it will be displayed as shown above. If I had NOT reused this view, my Office view would look like this:
@model SomeNameSpace.OfficeViewModel @Html.TextBoxFor(m => m.Person.FirstName) @Html.TextBoxFor(m => m.Person.LastName)
This will display as:
<input type="textbox" id="Person_FirstName" name="Person.FirstName" /> <input type="textbox" id="Person_LastName" name="Person.LastName" />
Note that the name attribute has the Person prefix property. So, if I use the RenderPartial parameter and pass to Model.Person, will the connecting device know where to bind FirstName and LastName in OfficeViewModel?
If the modelβs middleware is smart enough to check the properties of the properties, what happens when I have ManagerModel and EmployeeModel in OfficeViewModel, and both of them have properties named FirstName and LastName?
I hope I was simple and thankful in advance.