title: ASP.NET MVC 3 List<T> to IEnumerable<SelectListItem>
body: <p>I'm not currently happy with the way that my DropDownListFor() objects are populated. I'm attempting to find as generic way of populating IEnumerable as possible. This is what I have so far.</p>
  
  <p>Helper:</p>
  
  <pre><code>public static List&lt;SelectListItem&gt; ToSelectList(IDictionary&lt;string,string&gt; dictionaryItems, string selectedValue, string noSelection, bool search = false)
      {
          List&lt;SelectListItem&gt; items = new List&lt;SelectListItem&gt;();
  
          if (search)
          {
              items.Add(new SelectListItem { Selected = true, Value = "-1", Text = string.Format("-- {0} --", noSelection) });
          }
  
          foreach (var item in dictionaryItems)
          {
              items.Add(new SelectListItem
              {
                  Text = item.Key,
                  Value = item.Value,
                  Selected = selectedValue == item.Value ? true : false
              });
          }
  
          return items
              .OrderBy(l =&gt; l.Text)
              .ToList();
      }
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code>[HttpGet]
      public ActionResult Index()
      {
          var model = new CreateModel();
  
          var parentOrganisations = _orgs.FindBy(o =&gt; o.OwningOrganisationID == Globals.OrganisationID || o.ID == Globals.OrganisationID)
              .OrderBy(o =&gt; o.OrganisationName);
  
          Dictionary&lt;string, string&gt; items = new Dictionary&lt;string, string&gt;();
  
          foreach (var item in parentOrganisations)
          {
              items.Add(item.OrganisationName, item.ID.ToString());
          }
  
          model.Organisations = SelectLists.ToSelectList(items, "-1", "-- None -- ", true);
  
          return View(model);
      }
  </code></pre>
  
  <p>View:</p>
  
  <pre><code>&lt;div class="control-group"&gt;
          &lt;label class="control-label"&gt;Parent Organisation&lt;/label&gt;
          &lt;div class="controls"&gt;
              @Html.DropDownListFor(m =&gt; m.ParentOrganisationID, Model.Organisations, new { @class = "input-xlarge"})
              &lt;p class="help-block"&gt;Select a parent organisation to create a branch&lt;/p&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p>There seems to be A LOT of repetitive code in the controller. It takes a generic list, add's the Value and Text to a Dictionary and then uses that as input for a helper which builds up the select list to send as part of the model.</p>
  
  <p>Does anyone have any better ways to achieve this? I hate having bloat in my controller, and when I get several drop downs on a form this is exactly what will happen in this instance.</p>
  
  <p>Thanks,</p>
  
  <p>EDIT - Thanks to Kenneth's helper method, I've now consolidated the whole thing into one call in the controller:</p>
  
  <pre><code>            model.Organisations = _orgs.FindBy(o =&gt; o.OwningOrganisationID == Globals.OrganisationID || o.ID == Globals.OrganisationID)
              .OrderBy(o =&gt; o.OrganisationName)
              .ToList()
              .ToSelectList(org =&gt; org.OrganisationName, org =&gt; org.ID.ToString(), "-1", "None", true);
  </code></pre>