0

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" })
Community
  • 1
  • 1
Samra
  • 1,815
  • 4
  • 35
  • 71
  • `Id = (int)e`, but why do you want it to be a number? –  Mar 29 '17 at 04:29
  • did id = (int)e but it gave parse error. cannot convert type TEnum to int – Samra Mar 29 '17 at 04:32
  • i simply want to set value in dropdown list as the numbers and text as Include_ANY_MatchingIndicator etc – Samra Mar 29 '17 at 04:33
  • similar question is here http://stackoverflow.com/questions/1110070/how-to-get-the-values-of-an-enum-into-a-selectlist – Samra Mar 29 '17 at 04:34
  • Use `Id = Convert.ToInt32(e)` –  Mar 29 '17 at 04:39
  • But still not clear why you want it to be a number. To generate your dropdownlist in the view, it should be `@Html.DropDownListFor(m => m.IndicatorGroups. Model.IndicatorGroups.ToSelectList())` and if the value of `IndicatorGroups ` is `Include_ALL_MatchingIndicator ` then the 2nd option will be selected. –  Mar 29 '17 at 04:43
  • Cool it works! you can add the answer.. i am passing this number to my stored procedure..Are you saying this is not the best practice? – Samra Mar 29 '17 at 04:44
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/139317/discussion-between-stephen-muecke-and-samra). –  Mar 29 '17 at 04:45

1 Answers1

1

You can use Convert.ToInt32(e), so that the code becomes

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", Convert.ToInt32(enumObj));

However, you do not really want to do that. If the value of property IndicatorGroups is say IndicatorGroup.Exclude_ANY_MatchingIndicator, then your DropDownListFor() method will initially display the first option because the value of the property does not match any of the option values. Internally, the method builds a new IEnumerable<SelectListItem> based on the 2nd parameter, and sets the Selected property if the .ToString() value of the property matches the Value property of any SelectListItem - in your case "Exclude_ANY_MatchingIndicator" will not match 1, 2, 3, or 4 so the first option will be selected (because something has to be).

You have indicated in the comments that you want to save the selected value as anINT in the database, in which case, in your POST method, convert the bound value to an int. Assuming you select the 2nd option in the view, then the IndicatorGroups property of your model will be IndicatorGroup.Include_ALL_MatchingIndicator. To save the value as an int, use

int valueToSave = (int)model.IndicatorGroups; // returns 2