title: ASP.NET MVC (razor) Listbox multiselect values to string
body: <p>I have two multi-select list boxes; one with months and one with years such as the following:</p>
  
  <pre><code>@Html.ListBoxFor(m =&gt; m.IncludeGuestsArrivedInTimeframeMonths, Model.TimeframeMonths)
  @Html.ListBoxFor(m =&gt; m.IncludeGuestsArrivedInTimeframeYears, Model.TimeframeYears)
  </code></pre>
  
  <p>I'm saving the both values to the database as a comma separated list (string/nvarchar).  The values are being like this: <strong>Months:</strong> 1,3,7; <strong>Years:</strong> 2002,2005</p>
  
  <p>For some reason when I pull the values back out to the form, the months are pre-selecting in the listbox fine, but the years are not.</p>
  
  <p>Any ideas?</p>
  
  <hr>
  
  <p><strong>EDIT</strong> - Additional code samples:</p>
  
  <p><strong>Controller - Manage</strong></p>
  
  <pre><code>public ActionResult Manage(Guid id)
      {
          var list = _listService.GetList(id);
  
          var model = LoadModelFromObject(list);
          model.TimeframeMonths = GenerateMonthDropdown();
          model.TimeframeYears = GenerateYearDropdown(model.IncludeGuestsArrivedInTimeframeYears.Split(','));
          model.DaysOfWeek = GenerateDaysOfWeekDropdown();
  
          return View(model);
      }
  </code></pre>
  
  <p><strong>Controller - 'Helper'</strong></p>
  
  <pre><code>private IList&lt;SelectListItem&gt; GenerateYearDropdown(string[] selected)
      {
          var list = new List&lt;SelectListItem&gt;();
  
          var startYear = DateTime.Now.Year - 10;
  
          for (int idx = startYear; idx &lt; startYear + 11; idx++)
          {
              list.Add(new SelectListItem
                           {
                               Value = idx.ToString(),
                               Text = idx.ToString(),
                               Selected = selected != null &amp;&amp; selected.Contains(idx.ToString())
                           });
          }
  
          return list;
      }
  </code></pre>
  
  <p><strong>View</strong></p>
  
  <pre><code>@Html.CheckBoxFor(m =&gt; m.IncludeGuestsArrivedInTimeframe)
  @Html.LabelFor(m =&gt; m.IncludeGuestsArrivedInTimeframe)
  @Html.ListBoxFor(m =&gt; m.IncludeGuestsArrivedInTimeframeMonths, Model.TimeframeMonths)
  @*@Html.ListBoxFor(m =&gt; m.IncludeGuestsArrivedInTimeframeYears, Model.TimeframeYears)*@
  @Html.ListBox("IncludeGuestsArrivedInTimeframeYears", Model.TimeframeYears)
  </code></pre>