asp.net-mvc – ASP.NET MVC:使用EditorFor()和枚举的默认模板

我已经编写了一个EnumDropDownFor()帮助器,我想和EditorFor()一起使用。我刚刚开始使用EditorFor(),所以有点混淆如何选择模板。

我的Enum.cshtml编辑器模板如下:

<div class="editor-label">
    @Html.LabelFor(m => m)
</div>
<div class="editor-field">     
    @Html.EnumDropDownListFor(m => m)
    @Html.ValidationMessageFor(m => m)
</div>

明确定义要使用的模板的缺点是,有没有办法在将枚举传递给EditorFor()时使用认模板?

解决方法

您可以在Brad Wilson的博文中查看关于ASP.NET MVC中使用的 default templates的博文。当你有一个类型为Enum的model属性时,它是被渲染的字符串模板。所以你可以自定义这样的字符串编辑器模板:

〜/查看/共享/ EditorTemplates / String.cshtml:

@model object
@if (Model is Enum)
{
    <div class="editor-label">
        @Html.LabelFor(m => m)
    </div>
    <div class="editor-field">     
        @Html.EnumDropDownListFor(m => m)
        @Html.ValidationMessageFor(m => m)
    </div>
}
else
{
    @Html.TextBox(
        "",ViewData.TemplateInfo.FormattedModelValue,new { @class = "text-Box single-line" }
    )
}

然后在你的看法只是:

@Html.EditorFor(x => x.someEnumProperty)

相关文章

这篇文章主要讲解了“WPF如何实现带筛选功能的DataGrid”,文...
本篇内容介绍了“基于WPF如何实现3D画廊动画效果”的有关知识...
Some samples are below for ASP.Net web form controls:(fr...
问题描述: 对于未定义为 System.String 的列,唯一有效的值...
最近用到了CalendarExtender,结果不知道为什么发生了错位,...
ASP.NET 2.0 page lifecyle ASP.NET 2.0 event sequence cha...