5
votes

I have a C#.Net MVC3 web app. I am using Drop Down lists all over the place and having success. However, there are two that I am having trouble with. The only difference is that I am creating the SelectLists on the fly in the code rather than using Lookup tables. I use lookup tables for all the other Drop Downs. When I QuickWatch the SelectLists in the code, the correct Item has the Selected property value set to true. However, when the page loads, the item with the Selected property is not displayed. The first item is. Any ideas? This is one of those werid ones. I've tried both ways below. In both cases, the ViewBag.DateToYear and the SelectList DateToYear have the right values and 'Selected' properties set

1)

//Controller

        IList<string> dateToYear = new List<string>();
        for (int i = 0; i < numberYears; i++)
        {
            dateToYear.Add(DateTime.Now.AddYears(i).Year.ToString());
        }
        ViewBag.DateToYear = new SelectList(dateToYear,"2014")

//View

        @Html.DropDownList("DateFromYear", (SelectList)ViewBag.DateToYear )

2) //Controller SAME as above

//View

List<SelectListItem> DateToYear = new List<SelectListItem>();
foreach (var m in ViewBag.DateToYear)
{
    DateToYear.Add(new SelectListItem { Selected = @m.Selected, Value = @m.Text, Text = @m.Text });
} 

@Html.DropDownList("DateFromYear", DateToYear)
3
Have you tried a DropDownListFor? Why don't you have the SelectList options as part of either A) the ViewBag or B) a strongly typed view model? I am not seeing what you getting out of how you are approaching this.Jared Peless

3 Answers

6
votes

The problem is, SelectList works as designed. The bug is in the design. You may set the Selected Property in SelctedItem, but this will completely be ignored, if you traverse the list with the GetEnumerator() ( or if Mvc does that for you). Mvc will create new SelectfListItems instead.

You have to use the SelectList ctor with the SelectListItem[], the Text-Name, the Value-Name and the SelectedValue. Be aware to pass as SelectedValue the VALUE of SelectListItem, which you want to be selected, not the SelectListItem itself! Example:

Why not just do in the controller ?

Pseudo code :

//controller

ViewBag.DateToYear = new SelectList(new[]  
{ 
 new SelectListItem { Text = "10", Value = "10" }, 
 new SelectListItem { Text = "15", Value = "15" } 
 new SelectListItem { Text = "25", Value = "25" }, 
 new SelectListItem { Text = "50", Value = "50" }, 
 new SelectListItem { Text = "100", Value = "100" }, 
 new SelectListItem { Text = "1000", Value = "1000" }, 
}, "SomeText", "Value", "15");

The second option should be selected.

5
votes

Not sure if this is the case, but I had some problem with dropdownboxes not getting selected a few days ago.

My problem was: I have a @model.Options and I created a @html.dropdownlist("Options", @model.Options, [...]). However, because they had the same name it somehow conflicted. When I used @html.dropdownlist("anythingElse", @model.Options, [...]) it worked fine.

When I QuickWatched return View(model) the selected options seemed to be set correct too, so it took a while to figure that out. Maybe you have the same problem?

0
votes

Same thing was happening to me too, make sure you do not give same ID as the property name you gave on your Model. That is how I solved this issue...