Question

I have a drop down on my site that has a number of languages in it. When a user selects a different language, the page text reloads to that language. I would like the language drop down to change to reflect that by specifying the selected index of the selected language. However when ever I do this, the drop down in HTML does not change and always displays the first item in the list.

I have the following ViewModel :

public class BaseViewModel
{
  public BaseViewModel()
  {
  }

  public IEnumerable<SelectListItem> LanguageSelectListItems { get; set; }
}

The LanguageSelectListItems property is populated successfully when I construct a list of SelectListItems. When this is passed into the view, the selected language SelectListItem is set to true - in this case Swedish. All good so far.

But when the view calls Html.DropDownList() the selected languages' selected property changes to false. A couple of pictures might explain this better.

Before calling @Html.DropDownList("language", Model.LanguageSelectListItems)

before html.dropdownlist

Immediately after @Html.DropDownList("language", Model.LanguageSelectListItems)

after html.dropdownlist

You can see that the Selected property for the Swedish language has changed to false on the SelectListItem, and this is the resultant HTML :

<select name="language" id="language">
    <option value="2">English</option>
    <option value="3">Finnish</option>
    <option value="4">Swedish</option>
</select>

Any ideas why this might be happening ?

Edit: LanguageSelectListItems is being populated and the selected language being set using this function (where languages is an enumerable of language) :

public static IEnumerable<SelectListItem> GetLanguageList(int selectedIndex)
{
  var selectListItems = new List<SelectListItem>();
  foreach (var language in languages)
  {
    selectListItems.Add(new SelectListItem() { Text = language.Name, Value = language.Id });
  }

  if (selectedIndex >= 0)
  {
    selectListItems[selectedIndex].Selected = true;
  }

  return selectedListItems;
}
Was it helpful?

Solution

Well if anyone else finds themselves in this strange situation, the solution to my problem was this.

In the controller action method before returning the view with the SelectList drop down, there was this line of code :

ViewBag.language = Model.Language;

This seemed to be interfering with the way the SelectList was being constructed in the view :

@Html.DropDownList("language", Model.LanguageSelectListItems)

After removing this line from the controller the SelectList started defaulting to the correct value. This fixed both Html.DropDownList and Html.DropDownListFor. I also discovered this in another area of the site where the behaviour was the same, but this time a ViewData[""] was being used.

So if you do run into this problem, check your local areas for any uses of ViewBag and ViewData that use the same name as the property you are trying to construct the drop down for.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top