我有一个名为 Priority 的枚举类模型。我有一个优先级的编辑器模板和一个枚举的显示模板。在编辑器模板中,我创建了一个下拉列表,列出了所有可能的优先级值并在文本旁边显示字形图标。
问题是,在编辑器模板中,对于 where 的情况(e == Model)
,该行@Html.DisplayFor(model => e, "Enum")
不会调用显示模板,并且该选项仅显示图标而不显示文本。当我调试时,它通过那条线但没有通过显示模板。对于其他选项,它运作良好。
我的观点和模型看起来像这样:
优先级.cs:
public enum Priority {
[Display(Name = "Low Priority")]
Low = 1,
[Display(Name = "High Priority")]
High = 2,
[Display(Name = "Critical Priority")]
Critical = 3
}
Enum.cshtml(显示模板):
@model Enum
@if (EnumHelper.IsValidForEnumHelper(ViewData.ModelMetadata)) {
// Display Enum using same names (from [Display] attributes) as in editors
string displayName = null;
var selectList = EnumHelper.GetSelectList(ViewData.ModelMetadata, Model);
foreach (SelectListItem item in selectList) {
if (item.Selected) {
displayName = item.Text ?? item.Value;
}
}
// Handle the unexpected case that nothing is selected
if (String.IsNullOrEmpty(displayName)) {
if (Model == null) {
displayName = String.Empty;
}
else {
displayName = Model.ToString();
}
}
@Html.DisplayTextFor(model => displayName)
}
else {
// This Enum type is not supported. Fall back to the text.
@Html.DisplayTextFor(model => model)
}
Priority.cshtml(编辑器模板)
@model Priority
<select class="selectpicker" name="@ViewData.TemplateInfo.GetFullHtmlFieldName("")">
@foreach (Priority e in Enum.GetValues(Model.GetType())) {
<option value="@e"
@if (e == Model) {
<text>selected</text>
}
data-icon="@{
switch (e) {
case Priority.Low:
<text>glyphicon-arrow-down</text>
break;
case Priority.High:
<text>glyphicon-arrow-up</text>
break;
case Priority.Critical:
<text>glyphicon-exclamation-sign</text>
break;
default:
break;
}
}">
@Html.DisplayFor(model => e, "Enum")
</option>
}
</select>
我首先想到的是缓存。我从页面中删除了对显示模板的其他调用,但它仍然不起作用。