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" })
3
  • Have you read this answer? Does it help? stackoverflow.com/a/7229707/1559978 Commented Dec 8, 2014 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. Commented Dec 8, 2014 at 19:06
  • 1
    I figured it out. One of the suggestions did work "stackoverflow.com/questions/506001/…". I was just putting it in the wrong place. That's embarrassing. Commented Dec 8, 2014 at 19:10

1 Answer 1

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" })
Sign up to request clarification or add additional context in comments.

5 Comments

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.
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.
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.
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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.