Remove blank/empty entry at top of EnumDropDownListFor box
Asked Answered
D

3

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.

Darmstadt answered 22/3, 2016 at 15:10 Comment(0)
J
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?

Joyner answered 22/3, 2016 at 15:18 Comment(10)
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.Darmstadt
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.Darmstadt
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!Darmstadt
If you inspect the generated HTML, do you see an empty option? <option></option>?Joyner
This is what I see: <select class="form-control" data-val="true" data-val-required="The Type field is required." id="eventType" name="EventType"><option selected="selected" value=""></option> <option value="0">Option 1</option> <option value="1">Option 2</option> <option value="2">Option 3</option> </select> - there appears to be a selected option with a value "" which must be causing the problem.Darmstadt
This is just a guess, but is your model.EventType nullable? Not sure if this is what is adding the empty option.Joyner
Aha! How stupid of me - yes it was and problem solved. Thanks for your help!Darmstadt
@AndrésNava-.NET Please write your last comment as part of the answer.Atalayah
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.Kuhlmann
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.Buddy
W
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"  })
Wordbook answered 4/12, 2018 at 9:27 Comment(0)
S
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;
    }
Sweated answered 28/11, 2018 at 10:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.