1

I have enum with values and Display property using DataAnnotations in the dropdownlist value of Display property is shown correctly but when retreiving data from Database it shows value not Display property Text assigned using DataAnnotations.How can i get display value in my view. My Enum

public enum CareerLevel
        {
            [Display(Name = "Entry Level")]
            Level1,
            [Display(Name = "Experienced Professional")]
            Level2,
            [Display(Name = "Department Head")]
            Level3      
        }

here is my view where i want to display values like "Entry Level"

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

It shows Level1 instead of Entry Level . What change should i make in my View or enum??

Fahad
  • 128
  • 1
  • 3
  • 12
  • Could the extension in [this answer](http://stackoverflow.com/a/9329279/717088) be used to solve your problem? – Lars Kristensen Jun 20 '16 at 10:43
  • I believe the cleanest implementation is to create a new display template format linked in the duplicate flag, that way your views are far cleaner than calling extension methods in the views. – user1666620 Jun 20 '16 at 11:23
  • Impementing this solved my problem without any changes in the view http://www.codeproject.com/Articles/776908/Dealing-with-Enum-in-MVC – Fahad Jun 20 '16 at 13:21

2 Answers2

0

You can create an extension method to do this;

public static class Extensions
{

public static TAttribute GetAttribute<TAttribute>(this Enum enumValue) 
        where TAttribute : Attribute
{
    return enumValue.GetType()
                    .GetMember(enumValue.ToString())
                    .First()
                    .GetCustomAttribute<TAttribute>();
}
}

You can use it like this;

var level = CareerLevel.Level1

var name = level.GetAttribute<DisplayAttribute>().Name;

In your own code you can use it something like this;

@Html.DisplayFor(modelItem => item.CareerLevel.GetAttribute<DisplayAttribute>().Name)

Your Extensions class namespace must be referenced whereever you want to use the extension method.

mark_h
  • 5,233
  • 4
  • 36
  • 52
0

In my case following extension methods have used:

public static class MVCExtentions
{

public static string GetInputName<TModel, TProperty>(Expression<Func<TModel, TProperty>> expression)
{
    if (expression.Body.NodeType == ExpressionType.Call)
    {
        MethodCallExpression methodCallExpression = (MethodCallExpression)expression.Body;
        string name = GetInputName(methodCallExpression);
        return name.Substring(expression.Parameters[0].Name.Length + 1);

    }
    return expression.Body.ToString().Substring(expression.Parameters[0].Name.Length + 1);
}

private static string GetInputName(MethodCallExpression expression)
{
    MethodCallExpression methodCallExpression = expression.Object as MethodCallExpression;
    if (methodCallExpression != null)
    {
        return GetInputName(methodCallExpression);
    }
    return expression.Object.ToString();
}

public static MvcHtmlString EnumDropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression) where TModel : class
{
    string inputName = GetInputName(expression);
    var value = htmlHelper.ViewData.Model == null ? default(TProperty): expression.Compile()(htmlHelper.ViewData.Model);
    return htmlHelper.DropDownList(inputName, ToSelectList(typeof(TProperty), value.ToString()));
}
}

In the view then I use the following notation:

 @Html.EnumDropDownListFor(model => model.CareerLevel, new { @class = "form-control" })

My ViewModel looks as follows:

[EnumDataType(typeof(CareerLevel))]
[Display(Name = "Level")]
[DefaultValue(CareerLevel.Level1)]
public CareerLevel CareerLevelType { get; set; }

I hope I could you help a little ...

Carsten Cors
  • 2,505
  • 2
  • 14
  • 21