Remove blank/empty entry at top of EnumDropDownListFor box

Viewed 7814

I am rendering a drop down list box using my enums and I only have 3 options, but for some reason it is displaying four. The top and default option is simply blank/empty and I want this removed. I want the top/default value to be 'Option1'.

Enums:

public enum EventType
{
    [Display(Name = "Option 1")]
    Option1,

    [Display(Name = "Option 2")]
    Option2,

    [Display(Name = "Option 3")]
    Option3
}

View:

@Html.EnumDropDownListFor(model => model.EventType, null, new { @id = "eventType", @class = "form-control" })

Thanks for your help.

3 Answers

I also had the same problem.Solved this starting enum from 0 not 1 .

public enum EventType
{
[Display(Name = "Option 1")]
Option1,

[Display(Name = "Option 2")]
Option2,

[Display(Name = "Option 3")]
Option3
}

@Html.EnumDropDownListFor(model => model.EventType,      
              new {@id = "eventType", @class = "form-control"  })

I spent some time getting the above to work and was unsuccessful.

I found an alternative way :

HTML:

@Html.DropDownList("types",
               Helper.Gettypes(),
               new { @class = "form-control", @title = "---  Choose  ---" })

Code:

    public static List<SelectListItem> Gettypes()
    {
        var enums = Enum.GetNames(typeof(WorkLogType)).ToList();

        var selectLIst = enums.Select(x => new SelectListItem {Value = x, Text = x}).ToList();

        return selectLIst;
    }
Related