MVC Razor Html.Partial子模型

Rob*_*ell 0 c# asp.net-mvc razor asp.net-mvc-4

关于MVC Razor中的部分视图我有一个问题.任何帮助都非常受欢迎,这很可能是我错过的东西,但是在搜索时我找不到任何重复同样问题的东西.

所以我将视图绑定到视图模型.

public class Person
{
    public string Name { get; set; }

    public virtual ContactInformation ContactInformation { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

然后我有一个部分的视图来呈现联系信息模型.

<div>
    @Model.Name
</div>
<div>
    @Html.Partial("_ContactInformation", Model.ContactInformation)
</div>
Run Code Online (Sandbox Code Playgroud)

但是,"_ContactInformation"视图name<input>s 的属性中没有ContactInformation而呈现

通常razor将name属性绑定到类似:name ="ContactInformation.Address".但由于它是部分的,它被呈现为name ="Address".

我错过了什么或这是它的工作方式吗?

And*_*rei 5

你有两个选择.选项1 - 明确指定前缀:

@Html.Partial("_ContactInformation", Model.ContactInformation, new ViewDataDictionary
{
    TemplateInfo = new System.Web.Mvc.TemplateInfo { HtmlFieldPrefix = "ContactInformation" }
})
Run Code Online (Sandbox Code Playgroud)

选项2是将部分视图转换为模型的编辑器模板,而不是使用EditorFor辅助方法,它应该能够为您添加前缀:

@Html.EditorFor(m => m.ContactInformation)
Run Code Online (Sandbox Code Playgroud)