10

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.

Andy T
  • 10,223
  • 5
  • 53
  • 95
James
  • 1,471
  • 3
  • 23
  • 52

3 Answers3

13

Your second parameter is the "Option Label" which is used to set the first item in the dropdown. From the documentation: "The text for a default empty item"

Use an overload that doesn't take in the option label:

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

UPDATE

I just tried your code. When I do both:

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

And

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

I get:

enter image description here

The only time I get another option in the dropdown is when I pass in a string as the second parameter, such as "Select..."

Any chance you are doing something with javascript to add an item to the dropdown?

Andy T
  • 10,223
  • 5
  • 53
  • 95
  • I previously tried this and the blank value is still there, so I tried what was in my OP and the blank value is also still there, not sure how to get rid of it. – James Mar 22 '16 at 15:31
  • I don't want to have 'text for a default empty item'. When the text box 'appears', I want the first enum to be displayed and also displayed at the top of the list. – James Mar 22 '16 at 15:36
  • I don't think I am doing anything with javascript that would do that. I have managed to get Option1 selected bu default but when you click the dropdownlist there is still a blank above it which makes no sense! – James Mar 22 '16 at 15:48
  • If you inspect the generated HTML, do you see an empty option? ``? – Andy T Mar 22 '16 at 15:49
  • This is what I see: - there appears to be a selected option with a value "" which must be causing the problem. – James Mar 22 '16 at 15:51
  • 9
    This is just a guess, but is your model.EventType nullable? Not sure if this is what is adding the empty option. – Andy T Mar 22 '16 at 15:55
  • Aha! How stupid of me - yes it was and problem solved. Thanks for your help! – James Mar 22 '16 at 16:01
  • 1
    @AndrésNava-.NET Please write your last comment as part of the answer. – Mahmoud Jul 20 '16 at 06:22
  • If using ASP.NET MVC6 `EnumDropDownListFor<>` and the backing field in your viewmodel is of type Enum, then the blank default option will _still_ be shown even if your backing field is decorated with `[Required]` if the model is initialised with a null value - no matter which overload of `EnumDropDownListFor<>` you use in your view. The solution in this case (e.g. when initialising your model before rendering a view for _creating_ your entity) is to assign a default value to the enum type field in your view model constructor. – Tian van Heerden Jun 12 '18 at 18:51
  • 1
    In my case, the source of the problem was initialising an Enum value at 1 instead of 0, so the MVC control created a blank value in it's place. – Ciaran Gallagher Nov 23 '18 at 12:01
2

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"  })
0

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;
    }
ThunD3eR
  • 3,216
  • 5
  • 50
  • 94