0

I am trying to generate the following HTML using @Html.DropDownListFor I need to add a class to an option

<select class="selectpicker">
<option  title="food">Mustard</option>
<option  title="food" class="special">Ketchup</option>
<option  title="food">Relish</option>
<option  title="drink">fanta</option>
<option  title="drink" class="special">Red bull</option>
</select>
1
  • SelectListItem doesn't have the ability to add extra attributes to it Commented Aug 14, 2015 at 2:01

2 Answers 2

0

Only way is for loop. Cannot do this with @Html.DropDownListFor

<select class="selectpicker">
    @foreach (SelectListItem item in list)
    {
        <option title="@item.Value" @(new[]{"Ketchup","Red bull"}.Any(x => x == item.Text) ? "class=special" : "") @(item.Selected ? "selected" : "")>@item.Text</option>
    }
</select>
Sign up to request clarification or add additional context in comments.

Comments

0

@Html.DropDownListFor(x => x.PriorityID, (IEnumerable)ViewBag.PriorityID, new { @class = "dropdown" })

2 Comments

this adds a class to the select element not the option element

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.