title: DropDownList setting selected item in asp.net MVC
body: <p>I noticed what seems to me a bug in asp.net MVC or simply I am doing something wrong. I am currently using 1.0 so maybe this is something that will be addressed in the 2.0 release. But either way, here we go.</p>
  
  <p>When I my view model has a property which is the same name as the declared id for a drop down list, the selected item is ignored and the rendered html has nothing selected.
  Not sure if I did something wrong, but changing the name of the id fixes the problem. I simplified the example, hope it is clear, otherwise please let me know.</p>
  
  <p><strong>Here is my view where the declared ID is the same name as my list in the model:</strong></p>
  
  <pre><code>&lt;table border="0" cellpadding="0" cellspacing="0"&gt;
     &lt;tr&gt;
        &lt;td&gt;
           &lt;%= Html.DropDownList("IsMultipleServicers", Model.IsMultipleServicers) %&gt;
        &lt;/td&gt;
     &lt;/tr&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p><strong>And the rendered Html</strong></p>
  
  <pre><code>&lt;table border="0" cellpadding="0" cellspacing="0"&gt;
        &lt;tr&gt;
           &lt;td&gt;
               &lt;select id="IsMultipleServicers" name="IsMultipleServicers"&gt;
                  &lt;option value="false"&gt;No&lt;/option&gt;
                  &lt;option value="true"&gt;Yes&lt;/option&gt;
               &lt;/select&gt;
           &lt;/td&gt;
        &lt;/tr&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p><strong>Now lets make a small change. I will change the declared id to be something different.</strong></p>
  
  <p>Here is my View:</p>
  
  <pre><code>&lt;table border="0" cellpadding="0" cellspacing="0"&gt;
      &lt;tr&gt;
         &lt;td&gt;
            &lt;%= Html.DropDownList("MultipleServicers", Model.IsMultipleServicers) %&gt;
         &lt;/td&gt;
      &lt;/tr&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p><strong>And now the rendered html:</strong></p>
  
  <pre><code>&lt;table border="0" cellpadding="0" cellspacing="0"&gt;
     &lt;tr&gt;
        &lt;td&gt;
           &lt;select id="IsMultipleServicers" name="IsMultipleServicers"&gt;
              &lt;option value="false"&gt;No&lt;/option&gt;
              &lt;option selected="selected" value="true"&gt;Yes&lt;/option&gt;
           &lt;/select&gt;
        &lt;/td&gt;
     &lt;/tr&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>Notice that now I get a selected option which would be the second element in the List.</p>
  
  <p><strong>Here is my ViewModel just to tie everything together:</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.Mvc;
  
  namespace MVCProject.Models.ViewModels.Service
  {
      public class ServiceViewModel : ViewModel
      {
           public List&lt;SelectListItem&gt; IsMultipleServicers { get; set; }
      }
  }
  </code></pre>
  
  <p><strong>Here is my action:</strong></p>
  
  <pre><code>[AcceptVerbs(HttpVerbs.Get)]
  public virtual ActionResult Service()
  {
     return View(new ServiceViewModel()
     {
        IsMultipleServicers = BuildBooleanSelectList(true)
     };
  }
  
   private List&lt;SelectListItem&gt; BuildBooleanSelectList(bool isTrue)
   {
      List&lt;SelectListItem&gt; list = new List&lt;SelectListItem&gt;();
  
      if (isTrue)
      {
         list.Add(new SelectListItem() { Selected = false, Text = "No", Value = "false" });
         list.Add(new SelectListItem() { Selected = true, Text = "Yes", Value = "true" });
      }
      else
      {
         list.Add(new SelectListItem() { Selected = true, Text = "No", Value = "false" });
         list.Add(new SelectListItem() { Selected = false, Text = "Yes", Value = "true" });
      }
   return list;
    }
  </code></pre>