在视图中,我使用@ Html.EditorForModel()来显示模型的表单域,并且我尝试更改Object.cshtml EditorFor模板。下面的代码在MVC5中工作,但是使用.netcore时会返回以下errormessage:
"无法转换为' Microsoft.AspNetCore.Mvc.ModelBinding.ModelMetadata' to' Microsoft.AspNetCore.Mvc.ViewFeatures.ModelExplorer'"
Object.cshtml
@model object
@foreach (var prop in ViewData.ModelExplorer.Properties.Where(pm =>pm.ShowForEdit && !ViewData.TemplateInfo.Visited(pm)))
{
if (prop.HideSurroundingHtml)
{
@Html.Editor(prop.PropertyName)
}
else
{
@*@(prop.IsRequired ? "*" : "")*@
@Html.Editor(prop.PropertyName)
}
}
答案 0 :(得分:0)
几个星期前,我遇到了同样的问题。因此,我创建了一个最小化的Object.cshtml编辑器模板。
@model Object
@foreach (var prop in ViewData.ModelExplorer.Properties.Where(me => !ViewData.TemplateInfo.Visited(me)))
{
if (prop.Metadata.HideSurroundingHtml)
{
@Html.Editor(prop.Metadata.PropertyName);
}
else
{
<div class="form-group">
@Html.Label(prop.Metadata.PropertyName)
@Html.Editor(prop.Metadata.PropertyName, new { htmlAttributes = new { @class = "form-control" } }) @* hack for passig htmlAttributes retaken from: https://cpratt.co/html-editorfor-and-htmlattributes/ *@
@Html.ValidationMessage(prop.Metadata.PropertyName, new { @class = "text-danger field-validation-valid" })
</div>
}
}
我在此link
上也提供了此文件