title: MVC List<SelectListItem> with groups creating optgroup for each item
body: <p>I'm having an issue where I'm creating a <code>List&lt;SelectListItem&gt;</code> with <code>optgroups</code> but instead of creating an <code>optgroup</code> per group of <code>SelectListItem</code> it's creating a new <code>SelectListGroup</code> per <code>SelectListItem</code>. It's got me a bit confused because there aren't any duplicated <code>SelectListGroup</code>'s in my code.</p>
  
  <p>Here is an example:</p>
  
  <p>Expected Result:</p>
  
  <pre><code>&lt;select datatag="data-States=''" class="form-control filter-select" data-multi-select="" id="States" multiple="multiple" name="States"&gt;
      &lt;optgroup label="MA"&gt;
      &lt;option value="01602"&gt;01602&lt;/option&gt;
      &lt;option value="02743"&gt;02743&lt;/option&gt;
      &lt;option value="01107"&gt;01107&lt;/option&gt;
      &lt;/optgroup&gt;
      &lt;/select&gt;
  </code></pre>
  
  <p>Actual Result:</p>
  
  <pre><code>&lt;select datatag="data-States=''" class="form-control filter-select" data-multi-select="" id="States" multiple="multiple" name="States"&gt;
  &lt;optgroup label="MA"&gt;
  &lt;option value="01602"&gt;01602&lt;/option&gt;
  &lt;/optgroup&gt;
  &lt;optgroup label="MA"&gt;
  &lt;option value="02743"&gt;02743&lt;/option&gt;
  &lt;/optgroup&gt;
  &lt;optgroup label="MA"&gt;
  &lt;option value="01107"&gt;01107&lt;/option&gt;
  &lt;/optgroup&gt;
  &lt;/select&gt;
  </code></pre>
  
  <p>Method:</p>
  
  <pre><code> public ManifestFilterDropDownItem ReturnManifestFilterDataBasedOnTotalDataSet(IEnumerable&lt;ManifestTableItem&gt; data, bool isUserASR) {
              IEnumerable&lt;SelectListGroup&gt; stateGroups = data.Select(x =&gt; x.AddrState.ToUpper()).Distinct().Select(x =&gt; new SelectListGroup() {
                  Name = x
              });
  
              IList&lt;SelectListItem&gt; stateZipSelectListItems = data.GroupBy(x =&gt; x.AddrZip).Select(x =&gt; new SelectListItem() {
                  Text = string.IsNullOrWhiteSpace(x.Key) ? "Empty" : x.Key,
                  Value = string.IsNullOrWhiteSpace(x.Key) ? "" : x.Key,
                  Group = stateGroups.Where(y =&gt; y.Name == data.Where(p =&gt; p.AddrZip == x.Key).First().AddrState.ToUpper()).Single()
              }).OrderBy(x =&gt; x.Group.Name).ToList();
  
  
              var manifestItem = new ManifestFilterDropDownItem {
                  States = stateZipSelectListItems
              return manifestItem;
          }
  </code></pre>
  
  <p>ViewModel:</p>
  
  <pre><code>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 =&gt; $"data-{Name}=''";
          public IEnumerable&lt;SelectListItem&gt; SelectListItems { get; set; }
      }
  }
  </code></pre>
  
  <p>View:</p>
  
  <pre><code>@model FSVendor.Models.Manifest.ManifestFilterViewModel
  
  &lt;label&gt;States:&lt;/label&gt;
  @Html.DropDownList(Model.Name, Model.SelectListItems, new { @class = "form-control filter-select", data_multi_select = "", multiple = "multiple", @Model.DataTag })
  </code></pre>