Thursday, October 23, 2014

Create Separate Dropdowns for Day, Month, Year in ASP.NET MVC

Create a structure that will hold the Month, Day and Year fields:

public struct DatePart
    {
        public static IEnumerable<int> DayItems
        {
            get
            {
                return Enumerable.Range(1, 31);
            }
        }

        public static IEnumerable<String> MonthItems
        {

            get
            {
                return new System.Globalization.DateTimeFormatInfo().MonthNames;
            }
        }

        public static IEnumerable<int> YearItems
        {
            get
            {
                return Enumerable.Range(DateTime.Now.Year - 30, 30 + 1);
            }
        }
    }
Your model class should contain the following properties;

public int Day { get; set; }
public int Month { get; set; }
public int Year { get; set; }

     Create SelectListItem Date, Month and Year collections and add them to the ViewBag.

ViewBag.Days = DatePart.DayItems.Select(d => new SelectListItem { Text = d.ToString() });

 ViewBag.Months = DatePart.MonthItems.Select((m, i) => new SelectListItem
 {
    Value = (i + 1).ToString(),
    Text = m
 });

 ViewBag.Years = DatePart.YearItems.Select(y => new SelectListItem { Text = y.ToString() });
In the view, create dropdowns and populate them with collections in the ViewBag.
@Html.DropDownList("Day", (IEnumerable<SelectListItem>) ViewBag.Days)
@Html.DropDownList("Month", (IEnumerable<SelectListItem>) ViewBag.Months)
@Html.DropDownList("Year", (IEnumerable<SelectListItem>) ViewBag.Years)
It is important that the select element names match the property names in the model.

Now, in your controller method you can access the selected values as follows:
var selectedTime = new DateTime(model.Year, model.Month, model.Day);

15 comments:

  1. After looking into a handful of the blog articles on your site, I really like your technique of writing a blog. I book marked it to my bookmark site list and will be checking back in the near future. Take a look at my website as well and let me know your opinion.
    Logistics management software
    Oil and gas ERP
    RFID Solutions
    Human resources management software

    ReplyDelete
  2. Content is very clear and easily explained it is very interesting as well. I hope you post again soon.

    Selenium Training in Chennai

    ReplyDelete
  3. var selectedTime = new DateTime(model.Year, model.Month, model.Day);

    An exception will rise if you will select 30 or 31 in february

    ReplyDelete
  4. Thanks for the informative article. This is one of the best resources I have found in quite some time. Nicely written and great info. I really cannot thank you enough for sharing.
    best rpa training in bangalore
    rpa training in bangalore | rpa course in bangalore
    RPA training in bangalore
    rpa training in chennai
    rpa online training

    ReplyDelete
  5. Wonderful bloggers like yourself who would positively reply encouraged me to be more open and engaging in commenting.So know it's helpful.
    python course institute in bangalore
    python Course in bangalore
    python training institute in bangalore

    ReplyDelete
  6. I found your blog while searching for the updates, I am happy to be here. Very useful content and also easily understandable providing..
    Believe me I did wrote an post about tutorials for beginners with reference of your blog. 




    Selenium training in bangalore
    Selenium training in Chennai
    Selenium training in Bangalore
    Selenium training in Pune
    Selenium Online training

    ReplyDelete
  7. Thanks For sharing Your information The information shared Is Very Valuable Please Keep Updating Us Time Just went On reading Thae article Python Online Training DataScience Online Training AWS Online Training Hadoop Online Training

    ReplyDelete
  8. Really useful information. Thank you so much for sharing. It will help everyone. Keep Post.

    Best Microsoft Training in Bangalore for Microsoft, we provide the Microsoft training project with trainers having more than 5 Years of Microsoft training experience, we also provide 100% placement support.

    ReplyDelete
  9. Wow what a great blog, i really enjoyed reading this, good luck in your work. Otherwise anyone wants to learn 3D Max so contact here- +91-9311002620 or visit website- https://www.htsindia.com/Courses/cad-cam-cae/autocad-3ds-max-training-course

    ReplyDelete
  10. How to make money with online gambling - Work-to-Earn
    Online casino games are much like sports betting but there 온카지노 are many งานออนไลน์ ways you can 1xbet earn money from online gambling. Learn about them and how

    ReplyDelete