I have this bizarre question, but it is bothering me a lot. I have this css class:
class="form-control"
And I need to use it in my DropDown:
@Html.DropDownList("destination", ((SelectList)ViewBag.DropList).Select(t => new SelectListItem() { Text = t.Text, Value = t.Text, Selected = (t.Text == ViewBag.Destination)}), (SelectList)ViewBag.DropList)
Best Solution
It seems you are trying to set the
SelectedValue
of aSelectList
within theDropDownList
helper which is not possible.You have to set the
SelectedValue
property when you create theSelectList
. See following example.In the above code, I have set 2 as selected item when the
SelectList
is initialized. After that you can pass theSelectList
and HTML attributes as below.Thanks!
Solution: