Select Page
This entry has been published on 2016-07-12 and may be out of date.

Last Updated on 2016-07-12.

[:en]By default, @Html.DropDownListFor() is a convenient Razor helper to create a <select> tag containing <option> tags. But it is not possible to add custom attributes like “data-meow-xx=xxxx” to the option tags.

One solution is to create the <select> tag yourself:

<select name="@Html.NameFor(m => m.mycol)" id="@Html.IdFor(m => m.mycol)" class="form-control">
                                                  
    @foreach (var item in ViewBag.myoptions)
    {
        <option value="@item.name" data-src-mycustomstuff="myStuff/@item.name"
                       selected="@(Model != null && item.name == Model.mycol)">
            @item.name_friendly
        </option>
    }
</select>

Reference

 [:]