MVC剃刀Html.Partial子模型 [英] MVC Razor Html.Partial sub model

查看:113
本文介绍了MVC剃刀Html.Partial子模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有关于MVC剃刀局部视图的问题。任何帮助高度AP preciated,这是最有可能的事情我已经错过了,但我能找到什么,而搜索是有同样的问题重复。

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>

不过,_ContactInformation的观点是不ContactInformation中的名称属性呈现的&LT;输入&GT; 小号

通常剃须刀结合的name属性是这样的:名称=ContactInformation.Address。但因为它是一个局部的它被呈现为NAME =地址。

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?

推荐答案

您有两种选择。选项​​1 - 明确指定preFIX:

You have two options. Option 1 - specify the prefix explicitly:

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

选项2是将局部视图到一个编辑器模板模型,比使用 EditorFor helper方法,这应该是能够增加prefixes为您提供:

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:

@Html.EditorFor(m => m.ContactInformation)

这篇关于MVC剃刀Html.Partial子模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆