title: How to show multiple selected with asp.net mvc 3 and ListBoxFor?
body: <p>I have this VM properties</p>
  
  <pre><code>  public IList&lt;Guid&gt; SelectedEligiableCategories { get; set; }
    public IList&lt;SelectListItem&gt; EligiableCategories { get; set; }  
  </code></pre>
  
  <p>I have this helpers in my view</p>
  
  <pre><code>  @Html.LabelFor(x =&gt; x.EligibleCategoryFrmVm.SelectedEligiableCategories, "Eligible Categories:")
      @Html.ListBoxFor(x =&gt; Model.EligibleCategoryFrmVm.SelectedEligiableCategories, Model.EligibleCategoryFrmVm.EligiableCategories, new { @class = "eligibleCategoryListBox" }) 
  </code></pre>
  
  <p>I have this code in my controller</p>
  
  <pre><code>  List&lt;SelectListItem&gt; eligibleCategoriesListItems = Mapper.Map&lt;List&lt;EligibleCategory&gt;, List&lt;SelectListItem&gt;&gt;(eligibleCategories);
    foreach (var rewardTier in creditCard.RewardTiers)
          {
              CbRewardTierFrmVm rewardTierFrmVm = new CbRewardTierFrmVm();
              rewardTierFrmVm.EligibleCategoryFrmVm.EligiableCategories = eligibleCategoriesListItems;
  
              foreach (var ec in rewardTier.EligibleCategories)
              {
                  rewardTierFrmVm.EligibleCategoryFrmVm.SelectedEligiableCategories.Add(ec.Id);
              }
  
              vm.CbRewardTierFrmVm.Add(rewardTierFrmVm);
          }
  </code></pre>
  
  <p>Yet when I load up my view. None of values for my ListBox are selected. I am not sure why. If this was a selectList this would work as it would match up the SelectedEligiableCategories to the value in the list.</p>
  
  <p>I am not sure if this is because there is multiple selects </p>
  
  <p><strong>Edit</strong></p>
  
  <pre><code>&lt;select name="CbRewardTierFrmVm[63b504c0-0f9a-47ba-a8ff-db85f48d5f0f].EligibleCategoryFrmVm.SelectedEligiableCategories" multiple="multiple" id="CbRewardTierFrmVm_63b504c0-0f9a-47ba-a8ff-db85f48d5f0f__EligibleCategoryFrmVm_SelectedEligiableCategories" data-val-required="Must choose at least one eligible category." data-val="true" class="eligibleCategoryListBox ui-wizard-content ui-helper-reset ui-state-default" style="display: none;"&gt;
     &lt;option value="ed2bb5f9-4565-4f69-ab15-9fca011c0692"&gt;Gas&lt;/option&gt;
  &lt;/select&gt;
  </code></pre>
  
  <p>Do you think it is because I am using <a href="http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/">http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/</a> ?</p>
  
  <p><strong>Edit2</strong></p>
  
  <p>I gone ahead and make an example. I must be missing something(not sure what). When I use "Darin Dimitrov" it works.</p>
  
  <p>I switched the example to a dropdown as I am getting the same problem with it as well. </p>
  
  <p>In this example I am not using a viewmodel since my initial assumption was somehow the helper I was using from Steven Sanders might be effecting it so I was going off his example.</p>
  
  <p>This does not seem to be the case as I removed it and still get this problem.</p>
  
  <pre><code>  public class Gift
      {
          public string Name { get; set; }
          public double Price { get; set; }
          public string SelectedItem { get; set; }
          public IList&lt;SelectListItem&gt; Items { get; set; }
      }
  
  
   public ActionResult Index()
      {
          List&lt;SelectListItem&gt; items = new List&lt;SelectListItem&gt;
          {
  
                new SelectListItem {Value = "",Text ="--"},
                new SelectListItem {Value = "1",Text ="1"},
                new SelectListItem {Value = "2",Text ="2"},
          };
  
  
          var initialData = new[] {
              new Gift { Name = "Tall Hat", Price = 39.95, Items = items, SelectedItem = "2" },
              new Gift { Name = "Long Cloak", Price = 120.00, Items = items, SelectedItem = "1"  }
           };
  
          return View("Index3",initialData);
      }
  
  @model IList&lt;EditorDemo.Models.Gift&gt;
  
  @{
      ViewBag.Title = "Index3";
  }
  
  @for (int i = 0; i &lt; Model.Count; i++)
  {
       @Html.DropDownListFor(x =&gt; x[i].SelectedItem, new SelectList(Model[i].Items, "Value", "Text")) 
  }
  </code></pre>
  
  <p>It seems to not be able to handle when you put it in forloop and try it make more than one dropdown list.</p>