【问题标题】:How to create optGroup from multiselect (selectList)?如何从多选(selectList)创建 optGroup?
【发布时间】:2011-11-24 22:50:49
【问题描述】:

问:我如何在 multiselect 中创建 optGroups 给定以下条件:

模型(伪):

class Area {
  string Name;
  string Id;
  string GroupId;   // Want to group by this and display opt groups with my multiselect.
}

控制器:

ViewBag.AreaList = new SelectList(db.Areas.OrderBy(x => x.Name), "AreaId", "Name");

查看:

@Html.ListBox("AreaList", null) 

<script type="text/javascript">
$("#AreaId").multiselect({
    selectedText: "Selected: #",
    minWidth: 210,
    noneSelectedText: "",
    selectedList: 1
});
</script>

生成的 HTML(示例):

<select>
  <optgroup label="Group 1">
    <option value ="1">Option 1</option>
    <option value ="2">Option 2</option>
  </optgroup>
    <optgroup label="Group 2">
    <option value ="3">Option 3</option>
    <option value ="4">Option 3</option>
  </optgroup>
</select>

【问题讨论】:

    标签: jquery asp.net-mvc-3 multi-select


    【解决方案1】:
    <select>
    @foreach (var group in Model.AreaList.select(new {group:GroupID}).Distinct())
    {
        <optgroup>@group</optgroup>
        @foreach(var item in Model.AreaList.Where(r=>r.GroupID == group))
        {
             <option value="@id">@Name</option>
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-04
      • 1970-01-01
      • 1970-01-01
      • 2014-03-19
      • 2020-10-07
      相关资源
      最近更新 更多