title: Dropdown on masterpage. How to get the selected value right
body: <p>I have a website on with MVC, which has a dropdown in the masterpage. 
  The data in the database is linked to a Year. so every year we get a new clean DB to start with (or so it seems from the website, because the Year changed)</p>
  
  <p>The dropdown box lets you select the year you want to edit/see/go into. </p>
  
  <p>in the global.asax i have a method:</p>
  
  <pre><code>   public static SelectList schooljaarList() {
          NASDataContext _db = new NASDataContext();
          int schooljaarID = (from p in _db.Schooljaars
                              where p.Sch_Schooljaar == SCHOOLJAAR
                              select p.Sch_ID).First();
          return new SelectList(_db.Schooljaars.ToList(), "Sch_ID", "Sch_Schooljaar", schooljaarID);
      }
  </code></pre>
  
  <p>which makes the selectlist for the dropdown, and sets the default value to the current year.</p>
  
  <p>i've altered the routing so that the year is always visible in the URL</p>
  
  <pre><code>routes.MapRoute(
              "Default", // Route name
              "{schooljaar}/{controller}/{action}/{id}", // URL with parameters
              new { schooljaar = MvcApplication.SCHOOLJAAR, controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
              , new { schooljaar = @"(19|20)\d\d-(19|20)\d\d" }
          );
  </code></pre>
  
  <p>In the masterpage i added the control:</p>
  
  <pre><code>  &lt;%= Html.DropDownList("schselectr", MVC2_NASTEST.MvcApplication.schooljaarList())%&gt;
  </code></pre>
  
  <p>and when someone selects another year in the dropdown, i navigate to the matching url using jQuery</p>
  
  <pre><code>   $(document).ready(function() {
  
          $("#schselectr").change(function() {
              //alert(window.location.pathname);
              var oldpath = window.location.pathname;
              // alert($('option:selected', this).text());
              var newpath = "http://" + window.location.host + "/" + $('option:selected', this).text().trim() + window.location.pathname.substr(10);
              //alert(newpath);
              window.location = newpath;
          });
      });
  </code></pre>
  
  <p>till here everything works.</p>
  
  <p>The URL looks like this: <a href="http://localhost:50152/2010-2011/Lesgever" rel="nofollow noreferrer">http://localhost:50152/2010-2011/Lesgever</a>
  2010-2011 is the year.</p>
  
  <p>Now, when i am on this URL. i want that the selected value in the dropdown list will not be the current year, but the year that is visible in the URL.</p>
  
  <p>I've tried adding the following code in the jQuery's document.ready function</p>
  
  <pre><code>        alert(window.location.pathname.substr(1, 9).trim());
          document.getElementById("schselectr").value = "2010-2011";
          //$("#schselectr").val(window.location.pathname.substr(1, 9).trim());
  </code></pre>
  
  <p>but it doesn't work. the JS does not generate any errors.
  i'm using firefox.</p>
  
  <p>my questions:</p>
  
  <ul>
  <li><p>How do i get this to work, that the year of the URL will be the selected item in the dropdown, using javascript, jquery, or preferably even in MVC.</p></li>
  <li><p>How would you improve my working method for this system. (aka: the method in the global.asax, ...)</p></li>
  </ul>