MVC 6 VNext如何设置HtmlFieldPrefix? [英] MVC 6 VNext how to set HtmlFieldPrefix?
问题描述
我的视图分为部分视图.因此,在提交时,我的模型未正确重建.
My view is divided into partial views. Hence, at the time of submission my model isn't reconstructed correctly.
页面视图显示员工数据,其中Employee.Contactinfo是_contactInfo局部视图的模型,该视图又具有_phoneInfo局部视图,以呈现模型为Employee.ContactInfo.PhoneInfo的电话信息.
The page view displays employee data, where Employee.Contactinfo is the model of _contactInfo partial view, which again has a partial view _phoneInfo to render phone info having model Employee.ContactInfo.PhoneInfo.
现在问题出在属性名称上.呈现时的Employee.ContactInfo.PhoneInfo.Contact1名称为"Contact1",因此,在提交模型时未正确创建模型,我得到Employee的原始数据,但诸如ContactInfo之类的复杂类型为null.
Now the problem is with the name of properties. Employee.ContactInfo.PhoneInfo.Contact1 at the time of rendering has name "Contact1", hence at the time of submission the model isn't created appropriately, I get primitive data of Employee but complex type like ContactInfo is null.
我认为解决方案是在渲染局部视图时添加前缀.如何在MVC 6中执行以下操作?
I think the solution is to add the prefix at the time of rendering the partial view. How can I perform the following in MVC 6?
employee.cshtml
employee.cshtml
@model Employee
<% Html.RenderPartial("_conctactInfo", Model.ContactInfo, new ViewDataDictionary
{
TemplateInfo = new System.Web.Mvc.TemplateInfo { HtmlFieldPrefix = "ContactInfo" }
})
%>
_contactInfo.cshtml
_contactInfo.cshtml
@model ContactInfo
<% Html.RenderPartial("_phoneInfo", Model.PhoneInfo, new ViewDataDictionary
{
TemplateInfo = new System.Web.Mvc.TemplateInfo { HtmlFieldPrefix = "PhoneInfo" }
})
%>
_phoneInfo.cshtml
_phoneInfo.cshtml
@model PhoneInfo
<input asp-for="@Model.Contact1" />
推荐答案
这是解决方案,
namespace Website1.Extensions
{
public static class HtmlHelper
{
public static IHtmlContent Partial(this IHtmlHelper htmlHelper, string partialViewName, object model, string prefix)
{
var viewData = new ViewDataDictionary(htmlHelper.ViewData);
var htmlPrefix = viewData.TemplateInfo.HtmlFieldPrefix;
viewData.TemplateInfo.HtmlFieldPrefix += !Equals(htmlPrefix, string.Empty) ? $".{prefix}" : prefix;
return htmlHelper.Partial(partialViewName, model, viewData);
}
public static Task<IHtmlContent> PartialAsync(this IHtmlHelper htmlHelper, string partialViewName, object model, string prefix)
{
var viewData = new ViewDataDictionary(htmlHelper.ViewData);
var htmlPrefix = viewData.TemplateInfo.HtmlFieldPrefix;
viewData.TemplateInfo.HtmlFieldPrefix += !Equals(htmlPrefix, string.Empty) ? $".{prefix}" : prefix;
return htmlHelper.PartialAsync(partialViewName, model, viewData);
}
}
}
employee.cshtml
employee.cshtml
@using Website1.Extensions;
@model Employee
@Html.Partial("_contactInfo", Model.ContactInfo, nameof(Model.ContactInfo))
_contactInfo.cshtml
_contactInfo.cshtml
@using Website1.Extensions;
@model ContactInfo
@Html.Partial("_phoneInfo", Model.PhoneInfo, nameof(Model.PhoneInfo))
_phoneInfo.cshtml
_phoneInfo.cshtml
@model PhoneInfo
<input asp-for="@Model.Contact1" />
这篇关于MVC 6 VNext如何设置HtmlFieldPrefix?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!