jQuery DatePicker With Month And Year In ASP.NET MVC

Many times as developers we use a DatePicker to choose a date of birth or date of anniversary, and developers use a normal DatePicker, but for selecting previous date, like a date in 1982, users have to click the previous button.
 
We can use a DatePicker with Month and Year Dropdown to make this job easy in ASP.NET MVC.
 
 
 
Let's implement this jQuery DatePicker in ASP.NET MVC.
 
Step 1: Create new ASP.NET Web Application and give the name 'WebjQueryDatePicker'.
 
 
Step 2:  From the Next Dialog, Select MVC Template and click on Ok.
 


Step 3: Now first let's create a Model Class of Student, for that right click on Models folder and add new class named 'Student.cs'. 
 

Step 4: Now add the following property in class. Here for Student DateOfBirth we will use datepicker; set the DataFormatString = "{0:dd/MMM/yyyy}")
  1. public class Student  
  2. {  
  3.     [Display(Name = "Student Name")]  
  4.     [Required]  
  5.     public string StudName { getset; }  
  6.     [Required]  
  7.     [Display(Name = "Date Of Birth")]  
  8.     [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MMM/yyyy}")]  
  9.     public DateTime StudDOB { getset; }  
  10.   
  11. }  
Don't forget to add reference.
  1. using System.ComponentModel.DataAnnotations;  
Step 5: Now right click on Controller Folder, add new Controller and give name 'StudentController'. 
 
 
 

Step 6:
Now let's create a view for the Student Class, for that right click on Index, then add View.

In the next dialog Select 'Create' Template, assign Model Class 'Student' and click on 'Ok' Button. 
 
 

This will create a View as Below.  
  1. @model WebjQueryDatePicker.Models.Student  
  2.   
  3. @{  
  4.     ViewBag.Title = "Index";  
  5. }  
  6.   
  7. <h2>Index</h2>  
  8.   
  9.   
  10. @using (Html.BeginForm())   
  11. {  
  12.     @Html.AntiForgeryToken()  
  13.       
  14.     <div class="form-horizontal">  
  15.         <h4>Student</h4>  
  16.         <hr />  
  17.         @Html.ValidationSummary(true, "", new { @class = "text-danger" })  
  18.         <div class="form-group">  
  19.             @Html.LabelFor(model => model.StudName, htmlAttributes: new { @class = "control-label col-md-2" })  
  20.             <div class="col-md-10">  
  21.                 @Html.EditorFor(model => model.StudName, new { htmlAttributes = new { @class = "form-control" } })  
  22.                 @Html.ValidationMessageFor(model => model.StudName, "", new { @class = "text-danger" })  
  23.             </div>  
  24.         </div>  
  25.   
  26.         <div class="form-group">  
  27.             @Html.LabelFor(model => model.StudDOB, htmlAttributes: new { @class = "control-label col-md-2" })  
  28.             <div class="col-md-10">  
  29.                 @Html.EditorFor(model => model.StudDOB, new { htmlAttributes = new { @class = "form-control" } })  
  30.                 @Html.ValidationMessageFor(model => model.StudDOB, "", new { @class = "text-danger" })  
  31.             </div>  
  32.         </div>  
  33.   
  34.         <div class="form-group">  
  35.             <div class="col-md-offset-2 col-md-10">  
  36.                 <input type="submit" value="Create" class="btn btn-default" />  
  37.             </div>  
  38.         </div>  
  39.     </div>  
  40. }  
  41.   
  42. <div>  
  43.     @Html.ActionLink("Back to List", "Index")  
  44. </div>  
Step 7: For working with DatePicker, we have to add jQuery.UI Package, for that go to Tools, NuGet Package Manager, then Manage NuGet Packages for Solutions, and search for jQuery.UI.
 
 
Select jQuery.UI.Combined and click on Install and complete the installation of jQuery.UI.
 
 
Step 8: Now open your View and add the following script in it.
  1. @section Scripts {  
  2.   
  3.   
  4.     @Scripts.Render("~/bundles/jqueryui")  
  5.     @Styles.Render("~/Content/cssjqryUi")  
  6.     <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">  
  7.   
  8.     <script type="text/javascript">  
  9.   
  10.      $(document).ready(function () {  
  11.          $('input[type=datetime]').datepicker({  
  12.              dateFormat: "dd/M/yy",  
  13.              changeMonth: true,  
  14.              changeYear: true,  
  15.              yearRange: "-60:+0"  
  16.          });  
  17.   
  18.      });  
  19.     </script>  
  20.   
  21. }  
Important things here is:
  1. <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">  
That will apply a nice calendar UI to DatePicker. And input[type=datetime], This will match the Datatype and automatically identify where we have to place the DatePicker.

Step 9: Now open App_Start and Go to BundleConfig.cs and addthe below code.
  1.  
  2. //js    
  3. bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(  
  4.           "~/Scripts/jquery-ui-{version}.js"));  
  5.   
  6. //css    
  7. bundles.Add(new StyleBundle("~/Content/cssjqryUi").Include(  
  8.        "~/Content/jquery-ui.css"));  
Step 10: Now, run your application and check the Student DateofBirth Field, once you click it, it will display a nice jQuery Calendar with Month and Year selection.

output

Hope you like this article explaining jQuery DatePicker with Month and Year in ASP.NET MVC. 
 
Read more articles on ASP.NET:

Up Next
    Ebook Download
    View all
    Learn
    View all