sarsnake
sarsnake

Reputation: 27723

Dropdowlist in asp.net mvc with razor - set selected value and css class in one statement

I need to be able to define a drop down list in my cshtml. I have some that are populated with the arrays coming from my model view, so I define them as such

   @Html.DropDownList("dropdownlist-Id", Model.ValuesForDDL, new { @class = "ddl" })

How do I set the selected value as well? (Please note that I need to be able to set the class too).

Upvotes: 2

Views: 3192

Answers (1)

Yucel
Yucel

Reputation: 2673

you can set the selected value with giving a selectlist object to dropdownlist

new SelectList(items, "value", "text", selectedvalue);

ex:

 @Html.DropDownList("dropdownlist-Id", new SelectList(Model.ValuesForDDL, "id", "name", 5);
Model.ValuesForDDL, new { @class = "ddl" })

Now the selected item in combo is the item that has id 5

Upvotes: 2

Related Questions