MVC4-提交期间的部分视图模型绑定 [英] MVC4 - Partial View Model binding during Submit
问题描述
我有一个视图模型,该模型具有另一个子模型来呈现部分视图(如下).
I have view model which has another child model to render the partial view (below).
public class ExamResultsFormViewModel
{
public PreliminaryInformationViewModel PreliminaryInformation { get; set; }
public string MemberID { get; set; }
public string MemberName { get; set; }
public int PatientID { get; set; }
public string ConfirmationID { get; set; }
public bool IsEditable { get; set; }
#region Select Lists
public SelectList ProviderOptions { get; set; }
#endregion
}
public class PreliminaryInformationViewModel
{
public string ProviderName { get; set; }
public string ProviderID { get; set; }
public string ServiceLocation { get; set; }
}
此PreliminaryInformationViewModel视图模型在另一个视图模型中也用作子模型,因为可以在不同页面上更新此初步信息.
This PreliminaryInformationViewModel view model also used as a child models in another view model since this preliminary information can be updated at different pages.
因此,我将这些初步信息作为单独的部分创建,并包含在其他页面中.
So I created this preliminary information as a separate partial and to include in other pages.
@{Html.RenderPartial("_PreliminaryInformation", Model.PreliminaryInformation);}
在部分内
@model Web.Models.Preliminary.PreliminaryInformationViewModel
<div>
@Html.TextBoxFor(x => x.DateOfService })
</div>
但是问题是在提交过程中,由于HTML名称属性始终呈现为以下形式,因此该初步模型始终为null
But the problem is during submit this preliminary model is always null due to the reason HTML name attribute is always is rendered as
但是当我将父模型传递给部分模型时,如下所示.
but when I pass the parent model to the partial as below.
@model Web.Models.Exam.ExamResultsFormViewModel
<div>
@Html.TextBoxFor(x => x.PreliminaryInformation.DateOfService })
</div>
现在HTML元素生成为
Now the HTML element is generated as
<input type = 'text' name='PreliminaryInformation.DateOfService.DateOfService' id='PreliminaryInformation.DateOfService'>
,并且在提交期间正确绑定.
and it binds properly during the submit.
我了解MVC基于name属性值绑定元素值,但是第二种实现需要我为每个页面创建一个多个部分,这是我不喜欢的.
I understand MVC bind the element value based on the name attribute value, but the second implementation would need me to create a multiple partial for each page, which I don't like.
到目前为止,我找不到适合第一个实现的解决方案,有没有办法在提交第一个实现的过程中使初步的信息模型值绑定.
So far I couldn't find a solution to work with the first implementation, is there way I can make preliminary information model value bind during submit with the first implementation.
推荐答案
我知道有点晚了,但它可能对某人有帮助
I know its a bit late but it might help to someone
如果您有复杂的模型,仍然可以使用以下方法将其传递给部分模型:
If you have complex model, you can still pass it into partial using:
@Html.Partial("_YourPartialName", Model.Contact, new ViewDataDictionary()
{
TemplateInfo = new TemplateInfo()
{
HtmlFieldPrefix = "Contact"
}
})
我在其中定义了具有联系人"属性的模型.现在,HtmlFieldPrefix要做的是为每个模型添加属性绑定,以便模型绑定器可以找到父模型"
where I have defined model with property "Contact". Now what HtmlFieldPrefix do is add the property binding for each model "so the model binder can find the parent model"
有一篇关于它的博客文章: http://www.cpodesign.com/blog/bind-partial-view-model-binding-during-submit/
There is a blog post about it: http://www.cpodesign.com/blog/bind-partial-view-model-binding-during-submit/
.NET Core 2绑定
在.NET Core 2和MVC中,以上答案将不起作用,该属性不再可设置.
In .NET Core 2 and MVC the answer above will not work, the property is no longer settable.
解决方案非常相似.
@{ Html.ViewData.TemplateInfo.HtmlFieldPrefix = "Contact"; }
@await Html.PartialAsync("_YourPartialName", Model.Contact)
提交模型后,它将再次绑定.
after you can submit your model, it will bind again.
希望有帮助
这篇关于MVC4-提交期间的部分视图模型绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!