0

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" })
AtlasBowler
  • 267
  • 1
  • 8
  • 18
  • Have you read this answer? Does it help? http://stackoverflow.com/a/7229707/1559978 – Mike Rouse Dec 08 '14 at 18:59
  • I looked at that and a few other questions, but I haven't been able to get any of the suggestions to work. I keep getting errors in the HTML when I try to add something for a blank default. – AtlasBowler Dec 08 '14 at 19:06
  • 1
    I figured it out. One of the suggestions did work "http://stackoverflow.com/questions/506001/asp-net-mvc-dropdown-with-a-default-empty-option". I was just putting it in the wrong place. That's embarrassing. – AtlasBowler Dec 08 '14 at 19:10

1 Answers1

0

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" })
AtlasBowler
  • 267
  • 1
  • 8
  • 18
  • 2
    It's extremely bad form to have all this logic in your view. Use a view model to hold your select list or you can use `ViewBag`. `ViewBag` is kind of awful as well, but it's at least better than querying directly in your view. – Chris Pratt Dec 08 '14 at 19:18
  • I didn't write the view. I was just currently tasked with making this modification to it. We are setting up plans to do refactoring with out files so I'm sure it will be included. I do appreciate your input though. – AtlasBowler Dec 08 '14 at 19:26
  • 3
    Not to be belligerent, but planning to refactor is like planning to test. It's never going to happen unless you just do it. The best time to refactor is when you're knee-deep in the code. Refactoring should be an a continuous and never-ending process. Whenever you touch a piece of code you should not only make whatever changes need to be made, but also refactor anything that warrants it. That is how code is improved. Picking some future date to go and refactor all the code is a pipe dream. – Chris Pratt Dec 08 '14 at 19:43
  • It's working out pretty well so far. We have gotten a lot of files refactored already so its obviously not a "pipe-dream" because its happening. Thanks for your comments though. I'll make sure to mention your concerns at my next meeting. – AtlasBowler Dec 08 '14 at 20:27
  • Note you do not need the to create anonymous objcts using `.Select(...),`, nor do you need the last parameter (`Model.User.StateID`) of `SelectList` - the selection is based on the value `User.StateID` anyway. –  Dec 08 '14 at 21:44