MVC3 选择列表项。是否可以使用自定义 html 属性来装饰它?

iLe*_*ing 4 c# razor asp.net-mvc-3 drop-down-menu

如果我有这样的事情:

@{
   var cats = GetCategories();
   var selectList = from c in cats
   select new SelectListItem
   {
     Selected = (c.Id == Model.SessionCategory.Id),
     Text = c.Name,
     Value = c.Id.ToString(),
    };
  }
 @Html.DropDownList("categories", selectList)
Run Code Online (Sandbox Code Playgroud)

标记将是这样的:

 <select id="categories" name="categories">
    <option value="1">Category1</option>
    <option value="2">Category2</option>
    <option selected="selected" value="3">Category3</option>
 </select>
Run Code Online (Sandbox Code Playgroud)

现在,问题是:

是否可以为每个<option>标签添加额外的属性?我的意思是来自 Razor 模板。没有 jQuery 解决方法?

Jus*_*liz 5

我认为最实用的解决方案是一些 jQuery 或类似的东西..

<select id="categories" name="categories">
    @foreach (var item in selectList) {
        <option @if (item.Selected) { <text>selected="selected"</text>} value="@item.Value">@item.Text</option>
    }
</select>
Run Code Online (Sandbox Code Playgroud)

您显然可以根据需要添加属性...