MVC3 嵌套部分页面(和视图模型):如何绑定表单字段?
我有 2 个这样的视图模型:
public class ViewModel1 // maps to Model1
{
public string ViewModel1Desc { get; set; }
public ViewModel2 ViewModel2 { get; set; }
public ScheduleMasterEditViewModel()
{
ViewModel2= new ViewModel2();
}
}
public class ViewModel2 // maps to Model2
{
public string ViewModel2Desc { get; set; }
}
现在,我想要 ViewModel2 的部分页面并将其包含在 ViewModel1 的创建页面中:
Create.cshtml 看起来像这样
@model ViewModels.ViewModel1
@using (Html.BeginForm()) {
@Html.EditorFor(model => model.ViewModel1Desc )
@Html.Partial("~/Views/ViewModel2/_ViewModel2Create.cshtml", Model.ViewModel2)
}
_ViewModel2Create.cshtml 看起来像
@model ViewModels.ViewModel2
@Html.EditorFor(model => model.ViewModel2Desc )
问题是,在 Create 控制器上Model1,没有任何内容绑定到 ViewModel1.ViewModel2
我是否以正确的方式执行此操作,或者我应该像这样写出所有字段:
I have 2 view models like this:
public class ViewModel1 // maps to Model1
{
public string ViewModel1Desc { get; set; }
public ViewModel2 ViewModel2 { get; set; }
public ScheduleMasterEditViewModel()
{
ViewModel2= new ViewModel2();
}
}
public class ViewModel2 // maps to Model2
{
public string ViewModel2Desc { get; set; }
}
Now, I wanted to have a partial page for ViewModel2 and include that in the create page for ViewModel1:
Create.cshtml looks something like this
@model ViewModels.ViewModel1
@using (Html.BeginForm()) {
@Html.EditorFor(model => model.ViewModel1Desc )
@Html.Partial("~/Views/ViewModel2/_ViewModel2Create.cshtml", Model.ViewModel2)
}
_ViewModel2Create.cshtml looks like
@model ViewModels.ViewModel2
@Html.EditorFor(model => model.ViewModel2Desc )
The problem is, on the Create controller for Model1, nothing gets bound to ViewModel1.ViewModel2
Am I doing this the right way, or should I just write out all the fields like this:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ViewModel2 未绑定的原因是,当您查看生成的 HTML 时,您会注意到为此子模型创建的输入字段的名称不正确:
而正确的是:
原因是您的
_ViewModel2Create. cshtml
partial 不保留父级的导航属性上下文。因此,我建议您使用编辑器模板而不是部分调用:
然后将部分代码移至
~/Views/Shared/EditorTemplates/ViewModel2.cshtml
内:注意编辑器模板的位置:
~/Views/Shared/EditorTemplates
。这很重要。这是 ASP.NET MVC 查找它的地方。事实上,它会首先在~/Views/XXX/EditorTemplates
中查找,其中XXX
是您当前的控制器名称,以获取更具体的模板,如果在共享文件夹。另请注意文件的名称:ViewModel2.cshtml
。这也很重要,而且按照惯例是有效的。模板的名称实际上是属性的类型。您可以覆盖此:
或在视图模型上使用
UIHint
属性:然后使用
~/Views/Shared/EditorTemplates/_ViewModel2Create.cshtml
。The reason your ViewModel2 is not bound is because when you look at the generated HTML you will notice that the input fields that were created for this submodel have incorrect names:
whereas the correct is:
The reason for this is that your
_ViewModel2Create.cshtml
partial doesn't keep the navigational property context of the parent.For this reason I would recommend you to use editor templates instead of a partial call:
and then move your partial code inside
~/Views/Shared/EditorTemplates/ViewModel2.cshtml
:Notice the location of the editor template:
~/Views/Shared/EditorTemplates
. This is important. It is where ASP.NET MVC will look for it. In fact it will first look in~/Views/XXX/EditorTemplates
whereXXX
is your current controller name for more specific templates and if it doesn't find one look in the Shared folder. Also notice the name of the file:ViewModel2.cshtml
. This also is important and it works by convention. The name of the template is actually the type of the property.You could override this:
or using an
UIHint
attribute on your view model:and then have
~/Views/Shared/EditorTemplates/_ViewModel2Create.cshtml
.