2

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).

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.