Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does @Html.EnumDropDownListFor add a blank(0) selection when you have numerically defined values for an enum?

I have two enums:

public enum AnnouncementType
{
    All = 1,
    Corporate = 2,
    Franchisee = 3
}

public enum AnnouncementLevel
{
    Urgent,
    Normal
}

The first one, when using @Html.EnumDropDownListFor will add a blank option to the top of drop-down. The second behaves as expected and does not add the blank option. It has something to do with setting the values of the enum. Is there a way to get it to behave the same as not having values assigned?

like image 580
Danno Avatar asked Feb 13 '23 18:02

Danno


1 Answers

0 is the default value for an enum. In the second example, Urgent is 0. So it doesn't need to add one. In the first example, there is no 0, so it creates it for you.

like image 86
cadrell0 Avatar answered Feb 15 '23 13:02

cadrell0