本文介绍了支持下拉列表中的 optgroup .NET MVC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

继续这个问题 以编程方式创建下拉列表 我希望我的列表也有几个 optgroup 列表.这目前可能吗?

Continuing on from this question programmatically creating a drop down list I would like my list to have several optgroup lists too. Is this currently possible?

我知道我需要将 selectList 传递给 dropDownList,但不知道如何将文本、值、optgroup 添加到 selectList.

I know I need to pass a selectList to a dropDownList but do not know how to add text, value, optgroup to the selectList.

我希望产生最终结果:

<option value="">Please select</option>
  <optgroup label="Option A">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
  </optgroup>
  <optgroup label="Option B">
    <option value="a">A</option>
    <option value="b">B</option>
    <option value="c">C</option>
  </optgroup>
</option>

推荐答案

查看 www.codeplex.com 上的代码/aspnet,似乎 SelectList 和 DropDownList 扩展方法都不支持在选择中使用 OptGroup.看起来您需要编写自己的扩展方法并扩展 SelectListItem 以包含分组或在标记中手动生成选择.

Looking through the code on www.codeplex.com/aspnet, it doesn't appear that neither the SelectList nor the DropDownList extension method supports the use of OptGroup in a select. Looks like you'll need to write your own extension method and extend SelectListItem to contain the grouping or generate the select manually in markup.

这篇关于支持下拉列表中的 optgroup .NET MVC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 15:21