我的模型中有枚举,并且在视图的“显示名称”属性中未正确显示分配的值,但在显示值“ AB”
[display(Name) ="Value Assigned"]= AB
“已分配值”字符串。我应该如何更改我的枚举模型或视图?
在/ Views / Shared / DisplayTemplates文件夹中包含此编辑器模板代码,如果尚不存在,请创建该文件夹。
@model Enum
@* To display enum value in the view as it is givn by the data annotation Display[] method*@
@if (EnumHelper.IsValidForEnumHelper(ViewData.ModelMetadata))
{
// Display Enum using same names (from [Display] attributes) as in editors
string displayName = null;
foreach (SelectListItem item in EnumHelper.GetSelectList(ViewData.ModelMetadata, (Enum)Model))
{
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)
}
希望不需要进行其他任何更改。这解决了我的问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句