代码之家  ›  专栏  ›  技术社区  ›  Andrew

mvc list<selectListItem>组为每个项创建optGroup

  •  0
  • Andrew  · 技术社区  · 6 年前

    我有个问题 List<SelectListItem> 具有 optgroups 但不是创造一个 optgroup 每组 SelectListItem 它正在创造一个新的 SelectListGroup 选择列表项 .我有点困惑,因为没有任何复制品 选择列表组 在我的密码里。

    下面是一个例子:

    预期结果:

    <select datatag="data-States=''" class="form-control filter-select" data-multi-select="" id="States" multiple="multiple" name="States">
        <optgroup label="MA">
        <option value="01602">01602</option>
        <option value="02743">02743</option>
        <option value="01107">01107</option>
        </optgroup>
        </select>
    

    实际结果:

    <select datatag="data-States=''" class="form-control filter-select" data-multi-select="" id="States" multiple="multiple" name="States">
    <optgroup label="MA">
    <option value="01602">01602</option>
    </optgroup>
    <optgroup label="MA">
    <option value="02743">02743</option>
    </optgroup>
    <optgroup label="MA">
    <option value="01107">01107</option>
    </optgroup>
    </select>
    

    方法

     public ManifestFilterDropDownItem ReturnManifestFilterDataBasedOnTotalDataSet(IEnumerable<ManifestTableItem> data, bool isUserASR) {
                IEnumerable<SelectListGroup> stateGroups = data.Select(x => x.AddrState.ToUpper()).Distinct().Select(x => new SelectListGroup() {
                    Name = x
                });
    
                IList<SelectListItem> stateZipSelectListItems = data.GroupBy(x => x.AddrZip).Select(x => new SelectListItem() {
                    Text = string.IsNullOrWhiteSpace(x.Key) ? "Empty" : x.Key,
                    Value = string.IsNullOrWhiteSpace(x.Key) ? "" : x.Key,
                    Group = stateGroups.Where(y => y.Name == data.Where(p => p.AddrZip == x.Key).First().AddrState.ToUpper()).Single()
                }).OrderBy(x => x.Group.Name).ToList();
    
    
                var manifestItem = new ManifestFilterDropDownItem {
                    States = stateZipSelectListItems
                return manifestItem;
            }
    

    视图模型:

    using System.Collections.Generic;
    using System.Web.Mvc;
    
    namespace FSVendor.Models.Manifest {
        public class ManifestFilterViewModel {
            public ManifestFilterViewModel() {
    
            }
    
            public string Name { get; set; }
            public string DataTag => $"data-{Name}=''";
            public IEnumerable<SelectListItem> SelectListItems { get; set; }
        }
    }
    

    视图:

    @model FSVendor.Models.Manifest.ManifestFilterViewModel
    
    <label>States:</label>
    @Html.DropDownList(Model.Name, Model.SelectListItems, new { @class = "form-control filter-select", data_multi_select = "", multiple = "multiple", @Model.DataTag })
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   user3559349    6 年前

    查询正在创建新的 SelectListGroup 为每个人 SelectListItem ,即使每个 选择列表组 具有相同的值。

    修改查询以将数据分组,然后创建新的 选择列表组 每组

    // Initialize model
    var model = new ManifestFilterDropDownItem
    {
        States = new List<SelectListItem>
    }
    var states = data.GroupBy(x => x.AddrState); // group by state
    foreach (var group in states)
    {
        // Create a SelectListGroup
        var optionGroup = new SelectListGroup() { Name = group.Key };
        // Add SelectListItem's
        foreach (var item in group)
        {   
            model.States.Add(new SelectListItem()
            {
                Value = item.AddrZip,
                Text = item.AddrZip,
                Group = optionGroup
            })
        }
    }
    return model;
    

    或者,可以使用 SelectList 构造器

    var model = new ManifestFilterDropDownItem
    {
        States = new SelectList(data, "AddrZip", "AddrZip", "AddrState", null, null)
    };
    return model;
    

    作为旁注,不要使用 DropDownList() 创建 <select multiple> 是的。你需要使用 ListBoxFor() 以便双向模型绑定工作。参考 Why does the DropDownListFor lose the multiple selection after Submit but the ListBoxFor doesn't?