how to use enum with DescriptionAttribute in asp.net mvc

Santosh_Sams picture Santosh_Sams · Apr 13, 2015 · Viewed 13.9k times · Source

I am new to asp.net MVC. I am trying to use dropdown control on my view page, which populates from enum. I also want to add custom descriptions to dropdown values. I searched so many examples, but no one posted how to populated description on view page. Here is my code:

ViewModel:

public enum SearchBy
    {
        [Description("SID/PID")]
        SID = 1,
        [Description("Name")]
        Name,
        [Description("Birth Date")]
        DOB,
        [Description("Cause#")]
        Cause
    }

Index.cshtml

<div class="form-horizontal">
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group form-inline">
        @Html.LabelFor(model => model.searchBy, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EnumDropDownListFor(model => model.searchBy, "Search By", htmlAttributes: new { @class = "form-control" })
            @Html.TextBox("searchByVal", null, htmlAttributes: new { @placeholder = "SID / PID ", @class = "form-control" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @placeholder = "First Name", @class = "form-control" } })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @placeholder = "Last Name", @class = "form-control" } })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.DOB, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.DOB, new { htmlAttributes = new { @placeholder = "Birth Date", @class = "form-control" } })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.CauseNumber, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.CauseNumber, new { htmlAttributes = new { @placeholder = "Cause#", @class = "form-control" } })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Search" class="btn btn-block btn-primary" />
        </div>
    </div>
</div>

It is not populating the with description fields as mentioned in my SearchBy enum. see the image here. http://postimg.org/image/phdxgocj7/ Please help me, where I am making mistake. Thank you

UPDATE: I got solution for this from Nico. And I researched little bit on this. I am updating this post with solution because it may useful to others, who are new to MVC http://weblogs.asp.net/jongalloway//looking-at-asp-net-mvc-5-1-and-web-api-2-1-part-1-overview-and-enums

Thank you all. Enjoy coding..

Answer

Nico picture Nico · Apr 14, 2015

The Html helper EnumDropDownListFor or EnumDropDownList does not take into consideration the Description attribute decorations on the enum members. However by reviewing the source code:

Enum Dropdown List Helper: https://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.Mvc/Html/SelectExtensions.cs

Enum Helper Classes: https://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.Mvc/Html/EnumHelper.cs

The enum helper classes above are used to convert an Enum to a List<SelectListItem>. From the code below:

// Return non-empty name specified in a [Display] attribute for the given field, if any; field's name otherwise
private static string GetDisplayName(FieldInfo field)
{
    DisplayAttribute display = field.GetCustomAttribute<DisplayAttribute>(inherit: false);
    if (display != null)
    {
        string name = display.GetName();
        if (!String.IsNullOrEmpty(name))
        {
            return name;
        }
    }

    return field.Name;
}

You can see that in the method GetDisplayName it checks for the existence of the DisplayAttribute on the enum member. If the display attribute exists then the name is set to the result of DisplayAttribute.GetName() method.

Putting this together we can modify the enum to use the DisplayAttribute instead of the DescriptionAttribute and setting the Name property to the value you wish to display.

public enum SearchBy
{
    [Display(Name = "SID/PID")]
    SID = 1,
    [Display(Name = "Name")]
    Name,
    [Display(Name = "Birth Date")]
    DOB,
    [Display(Name = "Cause#")]
    Cause
}

This gives you the result you wish.

enter image description here

Hope this helps.