3

I am using ASP.NET MVC with WEB API.

I need to provide user to enter his date of birth, the input should be 3 drop downs

  • Select Day
  • Select Month and
  • Select Year

I am able to populate the dropdowns using the following code...

@Html.DropDownList("month", Enumerable.Range(1, 12).Select(i => new SelectListItem { Value = i.ToString(), Text = System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat.GetMonthName(i)}), "-- Month --")
@Html.DropDownList("day", Enumerable.Range(1, 31).Select(i => new SelectListItem { Value = i.ToString(), Text = i.ToString()}), "-- Day --")
@Html.DropDownList("year", Enumerable.Range(1920, DateTime.Now.Year).Select(i => new SelectListItem { Value = i.ToString(), Text = i.ToString()}), "-- Year --")

However I still will have to write custom js or C# code to make sure for example : February doesn't have more than 28 days for a particular year etc etc..

Is there any other method which you can suggest please... Please share your thoughts on this.

EDIT : I am not using Jquery Date Picker, as in my requirement I have to have 3 drop downs for day, month and year, every where else in the app for the rest of date selection I have used Jquery DatePicker.

Yasser Shaikh
  • 46,934
  • 46
  • 204
  • 281
  • 1
    Why don't you use jQuery DatePicker? – cuongle Jan 22 '13 at 08:02
  • use a datepicker plugin/control, it will handle that for you – phnkha Jan 22 '13 at 08:02
  • @CuongLe I am not using Jquery Date Picker, as in my requirement I have to have 3 drop downs for day, month and year, every where else in the app for the rest of date selection I have used Jquery DatePicker. – Yasser Shaikh Jan 22 '13 at 08:13
  • 1
    I know that this is very old question and I don't have any answer for this, but just used this code to add three dropdowns in my site, and wanted to tell that 3rd drop down is taking too much values, so if anyone using this code please correct 3rd dropdown as below @Html.DropDownList("year", Enumerable.Range(1920, DateTime.Now.Year - 1920).Select(i => new SelectListItem { Value = i.ToString(), Text = i.ToString()}), "-- Year --") – Sandesh Daddi May 23 '15 at 19:06

2 Answers2

0

You have to refresh days dropdown on month dropdown change and load days according to selected month

Aamir Nakhwa
  • 393
  • 1
  • 12
-2

That is definitely a good use case for a datepicker, such as the one from jQuery UI!

Edit:

Given your new requirement, you should have a look at How to fill cascading dropdownlist each other by using jquery in mvc 3?

Community
  • 1
  • 1
Yannick Blondeau
  • 9,465
  • 8
  • 52
  • 74