Search code examples
asp.net-mvcdefault-valuedropdownlistfor

Default Empty Option for MVC dropdown menu


I have the following DropDownListFor that displays a list of states for the user to choose from. How can I default the dropdown to be empty. It currently defaults to the first state in alphabetical order ("Alaska")

@Html.DropDownListFor(m => m.User.StateID,
    new SelectList(Model.User.States.Where(filter => filter.Active && (filter.CountryID == 1))
                    .Select(item => new
                        {
                            ID = item.StateProvinceID,
                            Description = item.Name
                        }),
                    "ID",
                    "Description",
                    Model.User.StateID),
    new { @data_bind = "value: user.StateID, enable:!isSiteUser()", tabindex = "10" })

Solution

  • I figured out that one of the posted questions did have the correct answer for my problem. I was just trying to do use it incorrectly. I was adding "string.Empty" to the wrong location. This is the solution.

    @Html.DropDownListFor(m => m.User.StateID,
    new SelectList(Model.User.States.Where(filter => filter.Active && (filter.CountryID == 1))
                    .Select(item => new
                        {
                            ID = item.StateProvinceID,
                            Description = item.Name
                        }),
                    "ID",
                    "Description",
                    Model.User.StateID),
                    string.Empty,
    new { @data_bind = "value: user.StateID, enable:!isSiteUser()", tabindex = "10" })