title: MVC DropDownList not showing selected value when driven by optional URL parameter
body: <p>I've got an Html.DropDownList that renders a list of months for the user to choose. If the page is first displayed (with no request params, basically), I want the current month to be selected by default. This is what I can't get to work. January is always selected initially,  no matter the current month.</p>
  
  <p>The somewhat odd thing about this is that month values are selected as offset values rather than actual month values. So, if the current month is April, then the list of months look like this:</p>
  
  <p>Jan = -3, Feb = -2, Mar = -1, Apr = 0, May = 1, Jun = 2 and so on</p>
  
  <p>(I did it this way so I can wrap around to other years without specifying a year directly, and so I could take advantage of default values for C# method params.)</p>
  
  <p>My controller action is declared like this:</p>
  
  <pre><code>public ActionResult Index(string mode, int? locationID = 0, int monthOffset = 0)
  </code></pre>
  
  <p>The monthOffset argument is supposed to represent the selected month. Notice it has a default value of 0. This is supposed to mean the current month + 0, basically. But when the HTML renders, it always shows the first item in the list selected (January), not the item with an actual value of 0 (April). January has a value of -3.</p>
  
  <p>I've verified that my SelectList object has the right month selected, but the HTML is just not rendering that selection. I tried setting up the monthOffset argument as a string in case the int type was confusing it, but that had no effect.</p>
  
  <p>I wonder if MVC sees the value 0 and assumes this always means the first item in the list, regardless of where "0" actually occurs in the SelectList...?</p>
  
  <p>Here's the actual GET action. A lot of it's irrelevant, I think. The main thing to look at is the MonthOffsetList function.</p>
  
  <pre><code>    public ActionResult Index(string mode, int? locationID = 0, int monthOffset = 0)
      {            
          ViewBag.LocationID = new SelectList(db.MyLocations(WebSecurity.CurrentUserName, true), "ID", "Name", locationID);
          // does not show current month on initial view
          ViewBag.MonthOffset = MonthOffsetList(monthOffset);
  
          if (mode == null || mode.Equals("m"))
          {
              DateTime viewDate = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1).AddMonths(monthOffset);
              int year = viewDate.Year;
              int month = viewDate.Month;
              DateTimeFormatInfo dtf = new DateTimeFormatInfo();
              ViewBag.ViewMonth = dtf.GetMonthName(month) + " " + year.ToString();
              return View(new CalendarModel(year, month));
          }
          else
          {
              ViewBag.ViewMonth = "Next 30 Days from this week";
              return View(new CalendarModel());
          }
      }
  
      private SelectList MonthOffsetList(int selectedValue)
      {
          List&lt;SelectListItem&gt; results = new List&lt;SelectListItem&gt;();
  
          int month = 0;
          int year = DateTime.Today.Year;
          DateTimeFormatInfo dtf = new DateTimeFormatInfo();
          for (int offset = 1 - DateTime.Today.Month; offset &lt; 13; offset++)
          {
              month++;
              if (month &gt; 12) 
              {
                  month = 1;
                  year++;
              }
              results.Add(new SelectListItem()
              {
                  Value = offset.ToString(),
                  Text = (year == DateTime.Today.Year) ? dtf.GetMonthName(month) : dtf.GetMonthName(month) + " " + year.ToString()                    
              });
          }
  
          return new SelectList(results, "Value", "Text", selectedValue);
      }
  </code></pre>
  
  <p>Here's the relevant part of the view:</p>
  
  <pre><code>@using (Html.BeginForm())
  {
  &lt;p class="center"&gt;
      Location:
      @Html.DropDownList("locationID", ViewBag.LocationID as SelectList, "(all)", new { onchange = "this.form.submit();" })
  
      View:        
      @Html.RadioButton("mode", "m", Request["mode"].IsEmpty() || Request["mode"].Equals("m"), new { id = "view_m", onclick="this.form.submit();" })
      @Html.Label("view_m", "Whole Month:")
  
      &lt;input type="submit" value="&amp;lt;&amp;lt;" onclick="this.form.MonthOffset.value--;" /&gt;
  // does not render current month correctly initially
      @Html.DropDownList("MonthOffset", ViewBag.MonthOffset as SelectList, new { onchange = "this.form.submit();" })        
      &lt;input type="submit" value="&amp;gt;&amp;gt;" onclick="this.form.MonthOffset.value++;" /&gt;
  
      @Html.RadioButton("mode", "w", !Request["mode"].IsEmpty() &amp;&amp; Request["mode"].Equals("w"), new { id = "view_w", onclick="this.form.submit();" })
      @Html.Label("view_w", "Continuous from this week")
  &lt;/p&gt;
  }
  </code></pre>