USing ASPNET MVC 4, Say i have an enum
public enum IndicatorGroup
{
Include_ANY_MatchingIndicator = 1,
Include_ALL_MatchingIndicator = 2,
Exclude_ANY_MatchingIndicator = 3,
Exclude_ALL_MatchingIndicator = 4
};
I use the following helper method to bind it to drop down list as described here: How do you create a dropdownlist from an enum in ASP.NET MVC?
public static class MyExtensions{
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 = e, Name = e.ToString() };
return new SelectList(values, "Id", "Name", enumObj);
}
}
Now i want that the selected value is a number (this is probably specified in the above code as)
Id = e ,
but when i get the selected value it returns me the text Exclude_ALL_MatchingIndicator instead of the number 4. How do i set it properly?
VIEW
@Html.DropDownListFor(m => m.IndicatorGroups, Model.IndicatorGroups.ToSelectList(), new { @id = "ddlIndicatorGroup" })