MVC List <selectlistitem>,具有为每个项目创建optgroup的组

时间:2018-06-25 19:45:35

标签: c# html asp.net-mvc selectlistitem

我遇到一个问题,我要用List<SelectListItem>创建一个optgroups,但不是每个optgroup组创建一个SelectListItem,而是创建一个新的{{每个SelectListGroup 1}}。这让我有些困惑,因为我的代码中没有重复的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>

ViewModel:

 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; }
    }
}

2 个答案:

答案 0 :(得分:1)

您的查询正在为每个SelectListGroup创建一个新的SelectListItem,即使每个SelectListGroup具有相同的值。

修改查询以对数据进行分组,然后为每个组创建一个新的SelectListGroup

// 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?

答案 1 :(得分:0)

我相信您的ManifestTableItem类至少具有以下结构:

class ManifestTableItem{
  public string AddrState { get ; set ; }
  public string AddrZip { get ; set ; }
}

要使状态在下拉列表中显示为一组,您需要使用以下重载:

new SelectList(data, nameof(ManifestTableItem.AddrZip), nameof(ManifestTableItem.AddrZip), null, nameof(ManifestTableItem.AddrState));