I try to make a view in ASP.NEt mvc 2 with a selectList.
I fill up the selectlist languages from my model (regDom)
listLangModel is a list of languages I retrieve from the database.
regDom.Languages = from l in listLangModel
select new SelectListItem { Text = l.Name, Value = l.ID };
In my view I have this
<div class="editor-label"> <%: Html.LabelFor(model =>> model.Languages) %> </div> <div class="editor-field"> <%: Html.DropDownListFor(model => model.Languages, Model.Languages) %> <%: Html.ValidationMessageFor(model => model.Languages) %> </div>
When I run, the languages are in the dropdownlist on my page.
But when I post it back to my server, the model doesn’t contain the languages anymore.
And I can’t check which language is selected on the page.
The user can select only a single language from the drop down list, so you cannot expect it to populate the
Languagesproperty of your model which is a collection.Add a property to hold the selected language in your model:
and then bind the drop down list to this property: