2016-06-30 28 views
0

我在我的模型枚舉和它沒有顯示在視圖中正確分配的顯示名稱屬性值,但它顯示的值「AB」我的枚舉值不能正常顯示ASP.NET MVC

[display(Name) ="Value Assigned"]= AB 

「值分配「字符串。我應該怎樣改變我的枚舉模型或視圖?

+0

哪個版本的MVC? –

+0

你可以分享你用來顯示該字段的剃鬚刀代碼嗎? –

+0

MVC 5. @ Html.DisplayFor(model => model.Industry)行業是我的enum – Jahson

回答

1

將此編輯器模板代碼包含在/ 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) 
} 

希望沒有其他的變化,這解決了我的問題。

0

不要忘記在模型的枚舉屬性中使用jUIHint("Enum")

Additional Resouce:codeproject.com

+0

你能給鏈接添加更多描述嗎? –