title: ASP.NET MVC dropdownlist not selecting value on render
body: <p>I have this annoying problem where my DropDownlist doesn't select the current value by default.</p>
  
  <p>Controller:</p>
  
  <pre><code>var YearsCycling = new SelectList(new List&lt;SelectListItem&gt;() 
  { 
      new SelectListItem(){ Value="1yr", Text="1yr"},
      new SelectListItem(){ Value="1-3yrs", Text="1-3yrs"}, 
      new SelectListItem(){ Value="3-5yrs", Text="3-5yrs"}, 
      new SelectListItem(){ Value="5-10yrs", Text="5-10yrs"}, 
      new SelectListItem(){ Value="10+yrs", Text="10+yrs"} 
  },
  "Value",
  "Text",
  new SelectListItem() { Value = "5-10yrs", Text = "5-10yrs", 
  Selected = true });
  ViewBag.YearsCycling = YearsCycling;
  </code></pre>
  
  <p>View:</p>
  
  <pre><code>&lt;%:Html.DropDownListFor(model=&gt;model.YearsCycling,(SelectList)ViewBag.YearsCycling,"-select-") %&gt;
  </code></pre>
  
  <p>but instead of selecting "5-10yrs", it just shows the "-select-" option and if I inspect the DOM source, none of the  elements are selected.</p>
  
  <p>UPDATE: I still don't know what the problem is, but I got it working for now by doing something ugly:</p>
  
  <pre><code>&lt;%
      var sl = (SelectList)ViewBag.YearsCycling;
  %&gt;
  &lt;select name="YearsCycling" id="YearsCycling"&gt;
  &lt;%foreach(var li in sl){  %&gt;
      &lt;option value="&lt;%:li.Value %&gt;" &lt;%if(li.Selected){%&gt;selected="true"&lt;%}%&gt;&gt;&lt;%:li.Text %&gt;&lt;/option&gt;
  &lt;%} %&gt;
  &lt;/select&gt;
  </code></pre>
  
  <p>This isn't the best solution, but if you've come to this question because you've been pulling your hair out, this should help.</p>