I am using MVC 5.
I have my ViewBag for list as
ViewBag.TitleList = new SelectList((new string[] { "Mr", "Miss", "Ms", "Mrs" }), contact.Title);
//contact.Title has selected value.
then I tried converting the array to SelectListItem ( to no avail)
On the View it looks like
@Html.DropDownListFor(model => model.Title, ViewBag.TitleList as SelectList, "Select")
also I tried
@Html.DropDownList("Title", ViewBag.TitleList as SelectList, "Select")
The list loads successfully but the Selected Value is Not selected
.
How to Fix this problem?
Update The culprit was ViewBag.Title matching my model.Title. Renamed my model property to something else and it worked. Arrgh!
@Html.DropDownListFor()
if the value ofmodel.Title
is (say) "Miss", then the 2nd option will be selected (you do not need the 2nd parameter in theSelectList
constructor). Check that the value ofTitle
matches exactly one of the option values. – Mesopotamia