Custom HTML attributes on SelectListItems in MVC2?

Posted by blesh on Stack Overflow See other posts from Stack Overflow or by blesh
Published on 2010-05-20T13:50:41Z Indexed on 2010/05/25 18:11 UTC
Read the original article Hit count: 910

I have a need to add custom HTML attributes, specifically classes or styles to option tags in the selects generated by Html.DropDownFor().

I've been playing with it, and for the life of me I can't figure out what I need to do to get what I need working.

Assuming I have a list of colors that I'm generating the dropdown for, where the option value is the color's identifier, and the text is the name... here's what I'd like to be able to see as output:

<select name="Color">
   <option value="1" style="background:#ff0000">Red</option>
   <option value="2" style="background:#00ff00">Green</option>
   <option value="3" style="background:#0000ff">Blue</option>
   <!-- more here -->
   <option value="25" style="background:#f00f00">Foo Foo</option>
</select

>

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about asp.net-mvc-2