I have a problem concerning Partial views in MVC Razor. Any help is highly appreciated, it's most likely something I've missed, but I could find nothing while searching that had the same problem replicated.
So I'm binding my view to a view model.
public class Person
{
public string Name { get; set; }
public virtual ContactInformation ContactInformation { get; set; }
}
And then I have a view with a partial to render the contact information model.
<div>
@Model.Name
</div>
<div>
@Html.Partial("_ContactInformation", Model.ContactInformation)
</div>
However, the "_ContactInformation" view is rendered without ContactInformation in the name
attribute of the <input>
s
Usually razor binds the name attribute to something like: name="ContactInformation.Address". But since it's a partial it gets rendered as name="Address".
Am I missing something or is this the intended way for it to work?
You have two options. Option 1 - specify the prefix explicitly:
Options 2 is to turn partial view into an editor template for your model and than use
EditorFor
helper method, that should be able to add prefixes for you: