1

I am using asp.net mvc. This might be a straight forward one. I am binding my drop-down list to an enum as follows

@Html.DropDownListFor(m => m.IndicatorGroups, Model.IndicatorGroups.ToSelectList(), new { @id = "ddlIndicatorGroup" })

Model is defined as follows

public class SearchControlViewModel
{
    ...
    public GlobalEnums.IndicatorGroup IndicatorGroups { get; set; }
    ...

}

ToSelectList function is defined as follows

public static SelectList ToSelectList<TEnum>(this TEnum enumObj) where TEnum : struct, IComparable, IFormattable, IConvertible
    {
        var values = from TEnum e in Enum.GetValues(typeof(TEnum))
                     select new { Id = Convert.ToInt32(e), Name = e.ToString() };
        return new SelectList(values, "Id", "Name", enumObj);
    }

Now i have added values with spaces to enum and i want to display these values instead of "values with underscores"

 public enum IndicatorGroup
    {
        [EnumMember(Value = "Include ANY MatchingIndicator")]
        Include_ANY_MatchingIndicator = 1,
        [EnumMember(Value = "Include ALL MatchingIndicator")]
        Include_ALL_MatchingIndicator,
        [EnumMember(Value = "Exclude ANY MatchingIndicator")]
        Exclude_ANY_MatchingIndicator,
        [EnumMember(Value = "Exclude ALL MatchingIndicator")]
        Exclude_ALL_MatchingIndicator
    };

How can i accomplish this?

Samra
  • 1,815
  • 4
  • 35
  • 71
  • 1
    In `MVC-5`, use [`EnumDropDownListFor`](https://msdn.microsoft.com/en-us/library/system.web.mvc.html.selectextensions.enumdropdownlistfor%28v=vs.118%29.aspx) to directly bind enum to dropdownlist. – mmushtaq Jul 14 '17 at 04:57
  • Refer [this answer](https://stackoverflow.com/questions/388483/how-do-you-create-a-dropdownlist-from-an-enum-in-asp-net-mvc) (using the `DescriptionAttribute`) –  Jul 14 '17 at 05:03

2 Answers2

1

In MVC we have EnumDropDownListFor which we can directly bind with our any enum, it is very similar to DropDownListFor

Sample ex:

public enum Courses
{
    [Display(Name = "ASP.NET")]
    ASPNet,
    [Display(Name = "C# .NET")]
    CSharp,
    [Display(Name = "Java")]
    Java,
    [Display(Name = "Objective C")]
    ObjectiveC,

}

Model:

public class Student
{
    [Key]
    public String StudentId { get; set; }

    [Display(Name="Student Name")]
    public String Student { get; set; }

    [Display(Name = "Languages")]
    public Courses Language { get; set; }


}

In View:

<div class="form-group">
    @Html.LabelFor(model => model.Languages, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EnumDropDownListFor(model => model.Languages, htmlAttributes: new { @class = "form-control" })
        @Html.ValidationMessageFor(model => model.Languages, "", new { @class = "text-danger" })
    </div>
</div>

Useful linK: http://www.advancesharp.com/blog/1163/mvc-enumdropdownlistfor-bind-with-enum-example

Thanks

Karthik

Karthik Elumalai
  • 1,574
  • 1
  • 11
  • 12
0

Since my requirement was simple enough so i replaced the string. I feel there must be a better way of doing the same.

public static SelectList ToSelectList<TEnum>(this TEnum enumObj) where TEnum : struct, IComparable, IFormattable, IConvertible
{
    var values = from TEnum e in Enum.GetValues(typeof(TEnum))
                 select new { Id = Convert.ToInt32(e), Name = e.ToString().Replace('_', ' ') };
    return new SelectList(values, "Id", "Name", enumObj);
}
Samra
  • 1,815
  • 4
  • 35
  • 71