There seem to be many different ways to create a dropdownlistFor. Viewbags etc.
I've been trying some different versions but always have problem adding all the "attributes" I need. It can't be possible that I should have to choose whether I want to use Distinct() OR add a class to my dropdownlistFor.
Please have a look at this, how would you go about adding a class to this:
@Html.DropDownListFor(model => model.CatDropTemp,
Model.Categories.Distinct().Select(kat => new SelectListItem { Text = kat, Value = kat }), "----Choose----")
Look at this overload of the DropDownListFor
. It has all the params you are using plus additional one, object htmlAttributes
. Basically that is how you assign arbitrary html attributes to the resulting tag. So if you wanted something like someAttribute
, you would pass new object { someAttribute = "value" }
.
Now class
is a bit different since this is a key word in C#. So for class you should precede it is @
. Like this:
@Html.DropDownListFor(
model => model.CatDropTemp,
Model.Categories.Distinct().Select(kat => new SelectListItem { Text = kat, Value = kat }),
"----Choose----",
new object { @class = "theClass" })