Html.DropDownList Selected Value Not Working (Using Constructor with IEnumerable<SelectListItem>)

You have the same problem here:

DropDownListFor Not Selecting Value

Problem is in your ViewBag property name. Because it is same as your property in Model it will not work. You should just change name of your ViewBag prop to something else, like:

ViewBag.NewsItemList = new SelectList(ViewBag.NewsItemId.Items, "Id", "Name", item.NewsItemId);

and on View

@Html.DropDownList("NewsItemId",ViewBag.NewsItemList as SelectList, string.Empty,
                           new { @class = "form-control" })

I tried lot of stuff but eventually got it working like this

In controller

int valittu_tila = 1;
var tapahtumantilat = new List<SelectListItem>();

tapahtumantilat.Add(new SelectListItem { Text = "Tapahtumaa kirjataan", Value = "0" });
tapahtumantilat.Add(new SelectListItem { Text = "Odottaa jonossa", Value = "1"});
tapahtumantilat.Add(new SelectListItem { Text = "Tapahtumaa käsitellään", Value = "2"});

ViewBag.tilalista = new SelectList(tapahtumantilat, "Value", "Text", valittu_tila);

And then in view I just put (I have the onchange event and class for dropdown too here but you can remove those if you want).

@Html.DropDownList("tuki_tila", ViewBag.tilalista as SelectList, "-- valitse tila --", new { @onchange = "executeticketsearch();", @class = "haku_dropdowns" })

(Sorry about finnish in the text values and variable names, but those shouldn't matter :D)


I did not like the fact that you can use

@Html.DropDownList("NewsItemID") 

and it works. Then you need to add the 'class' attribute and suddenly it breaks. I saw one place working and another not working using the syntax

@Html.DropDownList("NewsItemId",ViewBag.NewsItemId as SelectList, string.Empty,
                       new { @class = "form-control" })

When I researched this, I determined the ViewBag.NewsItemId was actually a null in the view. So a working syntax, with the Model and ViewBag fields having the same name is

@Html.DropDownList("NewsItemId", null, string.Empty,
                       new { @class = "form-control" })