@Html.DropDownListFor; How to set different background color for each item in DDL?

独自空忆成欢 提交于 2019-12-20 03:44:09

问题


I have a dropdown list (@html.DropDownListFor) wherein I am showing name of colors... I want to display each item having seperate background color.. Like, an item "Green" should be in green background, and "Yellow" in yellow background.

How can I achieve this?


回答1:


Don't forget that you can always write old fashioned HTML in a view (OMG!).

But, if you are using the code in multiple places, then write a helper that extends DropDownListFor to create your select with styling. If you are using it once, you can simply write something like:

<select>
    @foreach (var item in Model.Items)
    {
        <option style="background-color: @item.Color;" value="@item.Value">@item.Text</option>
    }
</select>



回答2:


See resolved solution here on ASP.NET Forum



来源:https://stackoverflow.com/questions/14170444/html-dropdownlistfor-how-to-set-different-background-color-for-each-item-in-dd

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!