2

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?

eth0
  • 4,977
  • 3
  • 34
  • 48
  • It's hard to tell, but for some reason equality is off... maybe because of encoding in the string, or maybe there is another character in the title that throws it off... Did you try writing out the title to the browser and verify it matches a value 100%? – Brian Mains May 12 '11 at 13:09
  • Yup, it matches 100% from browser output and in the database. – eth0 May 13 '11 at 09:49

2 Answers2

3

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>
Rom
  • 4,129
  • 23
  • 18
1

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.

Community
  • 1
  • 1
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • I'm extremely confused, it must be a bug somehow. I've had somebody check my code over and they are just as confused. I've resorted to manual HTML which works fine... – eth0 May 13 '11 at 09:49
  • @eth0, could you send me some sample project illustrating the problem? – Darin Dimitrov May 13 '11 at 10:00