DropDownListFor - why selected element is always the first?

Tschareck

I have a DropDownListFor in my View:

@model UserProfileViewModel

@Html.DropDownListFor(x => x.PreferredLangID,
    new SelectList(Model.PreferredLanguages,
        "LanguageID",
        "DisplayName",
        Model.PreferredLang))

Controller action:

 public ActionResult EditProfile()
 {
     List<Language> languages = Code.LanguageLogic.GetAllLanguages();
     UserProfile profile = Code.ProfileLogic.GetUserProfile();
     var viewModel = new UserProfileViewModel
     {
         UserID = profile.UserID,
         PreferredLanguages = languages,
         PreferredLang = profile.PreferredLang
     };
     return View(viewModel);
 }

I thought, that if I gave Model.PreferredLang as 4th paramater in SelectList's contructor, that element will be selected in dropdown, when page loads. But selected element is always the first one in list.

The Model:

public class UserProfileViewModel
{
    public Guid UserID { get; set; }
    public Language PreferredLang { get; set; }
    public int PreferredLangID { get; set; }
    public IEnumerable<Language> PreferredLanguages { get; set; }
    //...
}
SethMW

I don't know the details of the type, so I can't say for sure, but I'm guessing you might need:

@Html.DropDownListFor(x => x.PreferredLangID,
    new SelectList(Model.PreferredLanguages,
        "LanguageID",
        "DisplayName",
        Model.PreferredLang.PreferredLangId))

ETA:

Or, with your addition:

@Html.DropDownListFor(x => x.PreferredLangID,
    new SelectList(Model.PreferredLanguages,
        "LanguageID",
        "DisplayName",
        Model.PreferredLangId))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

DropDownListFor with an item value 0 always selected

@Html.DropDownListFor() Always Displays Blank even with a List Item selected

Why is it that when I resize my select the first element becomes selected?

First item in a List is always selected

react element selected is always the same

Styling all but the first selected element?

Always first filtered selected on Quasar Select

Selected items in DataGridViewComboBoxColumn always retrieve the first value

DropdownList value selected is always return first value

Datepicker selected value always goes on the first textbox

DropDownListFor selected value not working

I am trying to define a Linked list in Python, but why it is always going to add first element

Could someone explain why this function doesn't always return the first element of the list

asp.net mvc 5 DropDownListFor with empty first item is always Required

How to filter dropdownlistfor according to what has selected in first dropdownlist in asp mvc?

First element inserted in a Map is always read as null

Is the first element of a powershell array always "true"?

How to always drop element at first position?

Is 'array=X' always assigned to first element in bash?

SORTC always moves first element of array to the end

Jsoup always returns the index of the first appearance of an element

didSelectItemAtIndexPath not always called on first element in UICollectionView

Haskell group function / First element always missing

First element of double pointer array is always "garbage"

Django updateView always updating the first element in database

Element with :first-letter can't be selected

'dropdownlistfor' controller not taking the selected value

Save DropDownListFor multiple selected values

DropDownListFor pick an item and make it selected