Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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

I'm displaying values for a model normally like this:

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

The problem is that at one point I have this:

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

That line displays only status value which is set before from enum (1,2 or 3). Instead of that I need somehow to display name for that value. So, if status code is 2, I want to display 'No', not number 2.

I had the similar problem with getting enum names when I was populating dropdown list and I managed to solve it like this:

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

I am a little bit lost in how to get only that one name from the value of the enum.

Thank you for your help.

like image 729
Cristiano Avatar asked Mar 31 '13 15:03

Cristiano


People also ask

How do I find the enum name?

Enum. GetName(Type, Object) Method is used to get the name of the constant in the specified enumeration that has the specified value. Syntax: public static string GetName (Type enumType, object value);

How do I display the value of an enum?

An enumeration is a great way to define a set of constant values in a single data type. If you want to display an enum's element name on your UI directly by calling its ToString() method, it will be displayed as it has been defined.


2 Answers

It's not very clear from your question what's the underlying type of the Status property. If it is CheckStatus, then @Html.DisplayFor(modelItem => item.Status) will display exactly what you expect. If on the other hand it is an integer you could write a custom helper to display the proper 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 let's suppose that you wanted to bring this helper a step further and take into account the DisplayName attribute on 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's how you could extend our custom helper:

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));
    }
}
like image 75
Darin Dimitrov Avatar answered Oct 05 '22 22:10

Darin Dimitrov


Edit:

You can simply try this way. please try my below code in your controller class

controller:

int enumvalue=(int)(YourModel.Status)// this property must be integer value only
var checkStatusName= Enum.GetName(typeof(CheckStatus), enumvalue);
like image 34
Ramesh Rajendran Avatar answered Oct 05 '22 23:10

Ramesh Rajendran