【问题标题】:Edit Object.cshtml in .netcore for use with EditorForModel()在 .net 核心中编辑 Object.cshtml 以与 EditorForModel() 一起使用
【发布时间】:2017-09-05 14:07:07
【问题描述】:

在视图中,我使用 @Html.EditorForModel() 来显示模型的表单域,并且我正在尝试更改 Object.cshtml EditorFor 模板。下面的代码在 MVC5 中工作,但使用 .netcore 会返回以下错误消息:

“无法从 'Microsoft.AspNetCore.Mvc.ModelBinding.ModelMetadata' 转换为 '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)

    }
}

【问题讨论】:

  • 你解决过这个问题吗?

标签: asp.net-core-mvc .net-core


【解决方案1】:

几周前我遇到了同样的问题。所以我创建了非常最小化的 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 上也提供了这个文件

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-02
    • 1970-01-01
    • 1970-01-01
    • 2019-08-14
    • 2017-12-28
    相关资源
    最近更新 更多