I want to create two DropDownList
in a cascade using MVC3
(preferably Razor
) with C#
.
I would like to have one dropdown where you can choose the year and another one where you can choose a specific set of months depending on the selected year.
Let's put it simple. When I choose the current year (i.e. 2011) in the dropdown list "year", the dropdown list "month" gets populated with the months until the current month (i.e. March). For the other cases (other years) no restriction is given. Moreover it would be nice to "block" the dropdown list "month" before any element in the dropdown list "year" is selected.
I already looked in the Internet for some solutions, using jQuery
or even homemade approaches, but they all refer to past versions of MVC and some commands are deprecated in MVC3
.
As always you start with a model:
then a controller:
and finally a view:
Obviously you will notice that in my example I have hardcoded all the values. You should improve this logic by using notions like current year, current month, probably even fetch those values from a repository, etc... but for the purpose of the demonstration this should be enough to put you on the right track.