eth0
eth0

Reputation: 5137

Weird DropDownListFor / SelectListItem issue not setting default value

I'm pulling my hair out here. I'm using the DropDownListFor HTML helper in MVC3 with Enums like so:

@Html.DropDownListFor(model => model.Title, Enum.GetValues(typeof(ICS_Signup_Form.Models.Titles)).Cast<ICS_Signup_Form.Models.Titles>().Select(x => new SelectListItem { Text = x.ToString().ToFriendlyString(), Value = x.ToString() }), new { @class = "styled" })

My enum looks like so: public enum Titles { Mr, Dr, Miss, Mrs, Ms, Other };

If Model.Title equals "Other" then the drop down list doesn't select this value, there's no selected="selected" in the HTML. I've tried adding the property Selected to SelectListItem: Selected = (x.ToString() == Model.Title) and the expression works fine when I'm stepping through my code, as expected but the selected value is always "Mr" (the first in the list).

What's even weirder this works perfectly fine (as do the other 7 drop down boxes I have in my project):

@Html.DropDownListFor(model => model.BusinessStatus, Enum.GetValues(typeof(ICS_Signup_Form.Models.BusinessTypes)).Cast<ICS_Signup_Form.Models.BusinessTypes>().Select(x => new SelectListItem { Text = x.ToString().ToFriendlyString(), Value = x.ToString() }), new { @class = "styled" })

With the enum: public enum BusinessTypes { Charity, Government, Limited, LLP, Partnership, PLC, SoleTrader };

The difference? None.. Any ideas?

Upvotes: 2

Views: 4693

Answers (2)

Rom
Rom

Reputation: 4199

tldr; change the name of your DropDownList to something else

I had the same problem (albeit in ASPX engine, which I believe doesn't make any difference in this case). Here's how to repro it:

the codebehind (note "Selected = true" in the second item):

List<SelectListItem> teams = new List<SelectListItem>();
teams.Add(new SelectListItem { Text = "Slackers", Selected = false, Value = "0" });
teams.Add(new SelectListItem { Text = "Workers", Selected = true, Value = "1" });
ViewData["teams"] = teams;

the ASPX code:

<%: Html.DropDownList("team", (IEnumerable<SelectListItem>)ViewData["teams"]) %>

It reproes: you can see that the first item gets selected automatically and neither of the items have the "selected" option in HTML. Here's the generated HTML which reproes it:

<select id="team" name="team">
<option value="0">Slackers</option>
<option value="1">Workers</option>
</select>

Solution: Turns out some of the element names cause the ASPX rendering engine to behave differently. In my case, I just changed the DropDownList name from "team" to "defaultteam" -- and it started working:

Here's WORKING ASPX code:

<%: Html.DropDownList("defaultteam", (IEnumerable<SelectListItem>)ViewData["teams"]) %>

and here is the HTML generated by it:

<select id="defaultteam" name="defaultteam">
<option value="0">Slackers</option>
<option selected="selected" value="1">Workers</option>
</select>

Upvotes: 3

Darin Dimitrov
Darin Dimitrov

Reputation: 1038950

It's strange. I am unable to repro. Here's my working code.

Model:

public enum Titles { Mr, Dr, Miss, Mrs, Ms, Other };

public class MyViewModel
{
    public Titles Title { get; set; }
}

Controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel
        {
            Title = Titles.Other
        };
        return View(model);
    }
}

View:

@model MyViewModel

@Html.DropDownListFor(
    model => model.Title, 
    Enum.GetValues(typeof(Titles))
        .Cast<Titles>()
        .Select(x => new SelectListItem 
        { 
            Text = x.ToString(), 
            Value = x.ToString() 
        }), 
    new { @class = "styled" }
)

As expected, Other is preselected:

<select class="styled" id="Title" name="Title">
    <option value="Mr">Mr</option>
    <option value="Dr">Dr</option>
    <option value="Miss">Miss</option>
    <option value="Mrs">Mrs</option>
    <option value="Ms">Ms</option>
    <option selected="selected" value="Other">Other</option>
</select>

You might also find the following extension method useful.

Upvotes: 1

Related Questions