【问题标题】:EditorForModel - Duplicate labels when using editorforEditorForModel - 使用 editorfor 时标签重复
【发布时间】:2012-08-13 02:41:59
【问题描述】:

我在我的 ASP.NET MVC 应用程序中为不同的数据类型(字符串、日期时间等)使用了许多编辑器模板。例如,以下是我用于字符串的:

<div class="editor-label">
     @Html.Label("")
</div>
<div class="editor-field">
    @Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, new { @class = "text", placeholder = ViewData.ModelMetadata.Watermark })
    @Html.ValidationMessage("")
</div>

我想在我的视图中使用 EditorForModel,但是当我这样做时,它似乎为每个属性添加了自己的标签,导致标签重复(因为我的字符串编辑器模板中有一个标签)。除了从我的字符串编辑器模板(在本例中)中删除标签之外,还有什么方法可以告诉 editorformodel 不要插入标签?

【问题讨论】:

    标签: asp.net-mvc mvc-editor-templates editortemplates


    【解决方案1】:

    一种可能性是覆盖default object editor template (~/Views/Shared/EditorTemplates/Object.cshtml) 并删除它添加的标签:

    @if (ViewData.TemplateInfo.TemplateDepth > 1) 
    {
        @ViewData.ModelMetadata.SimpleDisplayText
    }
    else 
    {
        foreach (var prop in ViewData.ModelMetadata.Properties.Where(pm => pm.ShowForEdit && !ViewData.TemplateInfo.Visited(pm))) 
        {
            if (prop.HideSurroundingHtml) 
            {
                @Html.Editor(prop.PropertyName)
            }
            else 
            {
                <div class="editor-field">
                    @Html.Editor(prop.PropertyName)
                    @Html.ValidationMessage(prop.PropertyName)
                </div>
            }
        }    
    }
    

    【讨论】:

    • 您是否有机会详细了解它的作用?谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-27
    • 2020-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-26
    相关资源
    最近更新 更多