I'm having an issue where I'm creating a List<SelectListItem>
with optgroups
but instead of creating an optgroup
per group of SelectListItem
it's creating a new SelectListGroup
per SelectListItem
. It's got me a bit confused because there aren't any duplicated SelectListGroup
's in my code.
Here is an example:
Expected Result:
<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>
Actual Result:
<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>
Method:
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;
}
ViewModel:
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; }
}
}
View:
@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 })
SelectListGroup
for eachSelectListItem
? I select a distinct set ofAddrState
and then loop around then set the grouping based on the zip for the state? – Explorer