How to find the enumeration name for the corresponding value and use it in DisplayFor?

I need to display the enumeration name for the corresponding value inside the DisplayFor HtmlHelper. I have the following listing:

public enum CheckStatus { Yes = 1, No = 2, Maybe =3 } 

I show the values ​​for the model usually as follows:

 @Html.DisplayFor(modelItem => item.Name) 

The problem is that at some point I have this:

 @Html.DisplayFor(modelItem => item.Status) 

This line displays only the status value that is set before the enumeration (1,2 or 3). Instead, I need to somehow display the name for this value. So, if the status code is 2, I want to display "No", not number 2.

I had a similar problem with getting enumeration names when I populated a drop-down list and managed to solve it as follows:

 @Html.DropDownListFor(model => model.Item.Status, new SelectList(Enum.GetValues(typeof(Pro.Web.Models.Enums.CheckStatus)))) 

I lost a bit how to get only one name from an enum value.

Thank you for your help.

+4
source share
4 answers

It is not clear from your question what the basic type of the Status property is. If it is CheckStatus , then @Html.DisplayFor(modelItem => item.Status) display exactly what you expect. If, on the other hand, it is an integer, you can write a custom helper to display the correct value:

 public static class HtmlExtensions { public static IHtmlString DisplayEnumFor<TModel>(this HtmlHelper<TModel> html, Expression<Func<TModel, int>> ex, Type enumType) { var value = (int)ModelMetadata.FromLambdaExpression(ex, html.ViewData).Model; string enumValue = Enum.GetName(enumType, value); return new HtmlString(html.Encode(enumValue)); } } 

and then use it like this:

 @Html.DisplayEnumFor(modelItem => item.Status, typeof(CheckStatus)) 

and suppose you want to bring this helper further and take into account the DisplayName attribute for your enum type:

 public enum CheckStatus { [Display(Name = "oh yeah")] Yes = 1, [Display(Name = "no, no, no...")] No = 2, [Display(Name = "well, dunno")] Maybe = 3 } 

Here you can expand our user assistant:

 public static class HtmlExtensions { public static IHtmlString DisplayEnumFor<TModel>(this HtmlHelper<TModel> html, Expression<Func<TModel, int>> ex, Type enumType) { var value = (int)ModelMetadata.FromLambdaExpression(ex, html.ViewData).Model; string enumValue = Enum.GetName(enumType, value); var field = enumType.GetField(enumValue); if (field != null) { var displayAttribute = field .GetCustomAttributes(typeof(DisplayAttribute), false) .Cast<DisplayAttribute>() .FirstOrDefault(); if (displayAttribute != null) { return new HtmlString(html.Encode(displayAttribute.Name)); } } return new HtmlString(html.Encode(enumValue)); } } 
+7
source

Edit:

You can just try this way. try my code below in controller class

controller:

 int enumvalue=(int)(YourModel.Status)// this property must be integer value only var checkStatusName= Enum.GetName(typeof(CheckStatus), enumvalue); 
+3
source

Adapted @DarinDimitrov code as a display template.

If you

 public enum CheckStatus { [Display(Name = "oh yeah")] Yes = 1, [Display(Name = "no, no, no...")] No = 2, [Display(Name = "well, dunno")] Maybe = 3 } public class MyModel { public CheckStatus Status { get; set; } } 

enter the following data in Views / DisplayTemplates / Enum.cshtml:

 @using System.ComponentModel.DataAnnotations @model object @{ var value = Model.ToString(); var field = Model.GetType().GetField(value); if (field != null) { var displayAttribute = field .GetCustomAttributes(typeof(DisplayAttribute), false) .Cast<DisplayAttribute>() .FirstOrDefault(); if (displayAttribute != null) { value = displayAttribute.GetName(); } } } @value 

and now you can simply write in your view:

 @Html.DisplayFor(model => model.Status) 
+1
source

with razor / MVC you can get this for "free" if your property is not declared as an int type, but rather as a yoou enumeration type.

The problem I'm encountering with Darin's solution, although this works for me, if I use the int type, I could not get the appearance of the other @ Html.EditorFor (...) fields in the view.

+1
source

All Articles