title: Select all items in listbox on page load
body: <p>In my MVC application, I am retrieving a list of objects based on an <code>ID</code> and stuffing those objects into a Listbox via a SelectList.  Here is what I have:</p>
  
  <hr>
  
  <p><strong>C#</strong></p>
  
  <pre><code>ViewBag.SpecCatListBox = new SelectList(SelectListMethods.LstChosenSpecCat(incidentVm.ID), "Value", "Text");
  </code></pre>
  
  <hr>
  
  <p><strong>HTML/Razor</strong></p>
  
  <pre><code>@Html.ListBoxFor(model =&gt; model.LstSpecialCategories, (SelectList)ViewBag.SpecCatListBox, new { id = "SpecialCat-ListBox", @class = "form-control" })
  </code></pre>
  
  <p>On page load, the listbox is filled with the correct options, except that they're not selected.  Is there a way to do this without looping (which is what I've seen in other posts)?</p>
  
  <p>Any help is appreciated.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I've edited a few things, along with added a line of code.</p>
  
  <p><strong>C#</strong></p>
  
  <pre><code>ViewBag.SpecCatListBox = new SelectList(SelectListMethods.LstChosenSpecCat(incidentVm.ID), "Value", "Text", SelectListMethods.LstChosenSpecCat(incidentVm.ID));
  ViewBag.SpecCatIds = db.TBL_AssocIncidentSpecialCat.Where(x =&gt; x.IncidentId == incidentVm.ID)
      .Select(x =&gt; x.SpecialCategoriesId).ToList();
  </code></pre>
  
  <p><strong>HTML/Razor</strong></p>
  
  <pre><code>@Html.ListBoxFor(model =&gt; model.LstSpecialCategories, new MultiSelectList(ViewBag.SpecCatListBox, "Value", "Text", ViewBag.SpecCatIds), new { id = "SpecialCat-ListBox", @class = "form-control" })
  </code></pre>
  
  <p>This is selecting all of the options as needed, but is there a way to not use 2 Viewbag objects?</p>