Calendar Control Using jQuery UI In ASP.NET MVC 5

Background

Calender control is very useful in any application and in ASP.NET there are several default server control  to use but in MVC we have lot of options to use calendar control which all are client side control. In this article we will use Calender control in ASP.NET MVC with the help of jQuery UI library. So let us implement this requirement step by step,

Step 1 : Create an MVC application.
  1. "Start", then "All Programs" and select "Microsoft Visual Studio 2015".

  2. "File", then "New" and click "Project", then select "ASP.NET Web Application Template" and provide the Project a name as you wish and click on OK.

  3. Choose MVC empty application option and click on OK
Step 2: Add model class.

Right click on Model folder in the created MVC application and add class named EmpModel and write the following line of code,

EmpModel.cs
  1. using System;  
  2. using System.ComponentModel.DataAnnotations;  
  3.   
  4. namespace CalenderControlInMVCUsingjQuery.Models  
  5. {  
  6.     public class EmpModel  
  7.     {   [Display(Name ="Enter Date")]  
  8.         public DateTime EnterDate { getset; }  
  9.     }  
  10. }   
Step 3: Home controller.

Right click on Controller folder in the created MVC application and add the controller class, 

Now after selecting controller template, click on add button then the following window appears,



Specify the controller name and click on add button,

HomeController.cs
  1. using System.Web.Mvc;  
  2.   
  3. namespace CalenderControlInMVCUsingjQuery.Controllers  
  4. {  
  5.     public class HomeController : Controller  
  6.     {  
  7.         // GET: Home  
  8.         public ActionResult Index()  
  9.         {  
  10.             return View();  
  11.         }  
  12.     }  

In the above code, the only action result method named Index will handle the user request when the user request for the view named Index.cshtml. The same we will add later,

Step 4: Reference jQuery UI.

Reference jQuery UI css and js library reference as there are many ways to add the reference of jQuery library into our project. The following are some methods,
  • Using NuGet package manager , you can install library and reference into the project.

Right click on created MVC project and find Nuget Package manager option,

 

Now the following window will appear and search for the jQuery UI as,

 

Choose  appropriate version and click on Install button. It will install jQuery UI library to your project and library script file get added into the script folder of the created project which you can add reference into the project,
  • Use CDN library provided by Microsoft, jQuery, Google or any other which requires active internet connection.
  • Download jQuery files from jQuery official website and reference into the project.

In this example we will use jQuery CDN library.

Step 5: Create jQuery function to invoke datepicker function,
  1. <script>  
  2.   $(document).ready(function() {  
  3.       $("#EnterDate").datepicker();  
  4.   });  
  5. </script> 
To work above function don't forget to add the reference of the following jQuery CDN library as,
  1. <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">  
  2. <script src="//code.jquery.com/jquery-1.10.2.js"></script>  
  3. <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script> 
 We can overload datepicker function with many options but following are the most commonly used properties which are the following,
  • dateFormat: This property is used to set the date format for selected date from calendar.

  • changeMonth: This is a boolean property having the value true and false which decide whether the month dropdownlist is visible or not on calendar control.

  • changeYear: This is also a boolean property having the value true and false which decide whether the year dropdownlist to be visible or not in calendar control.

  • yearRange: It sets the year range for calendar control such as how many past and future years to display in calendar.

  • showOn: This property decided on which control Id does the calendar control will popup.
 Step 6: Add View.

Add strongly typed view named index from EmpModel class as :


After adding necessary code, files and logic the Index.cshtml code will look like the following,

Index.cshtml
  1. @model CalenderControlInMVCUsingjQuery.Models.EmpModel  
  2.   
  3. @{  
  4.     ViewBag.Title = "www.compilemode.com";  
  5. }  
  6. <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">  
  7. <script src="//code.jquery.com/jquery-1.10.2.js"></script>  
  8. <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>  
  9.   
  10. <script>  
  11.   $(document).ready(function() {  
  12.       $("#EnterDate").datepicker();  
  13.   });  
  14. </script>  
  15.   
  16. @using (Html.BeginForm())   
  17. {  
  18.     @Html.AntiForgeryToken()  
  19.       
  20.     <div class="form-horizontal">  
  21.          
  22.         <hr />  
  23.         @Html.ValidationSummary(true""new { @class = "text-danger" })  
  24.         <div class="form-group">  
  25.             @Html.LabelFor(model => model.EnterDate, htmlAttributes: new { @class = "control-label col-md-2" })  
  26.             <div class="col-md-10">  
  27.                 @Html.EditorFor(model => model.EnterDate, new { htmlAttributes = new { @class = "form-control" } })  
  28.                 @Html.ValidationMessageFor(model => model.EnterDate, ""new { @class = "text-danger" })  
  29.             </div>  
  30.         </div>  
  31.       
  32.        
  33.     </div>  

Now run the application and put mouse pointer into the textbox. You can see the calendar control will popup as in the following screenshot,

 

The above calendar by default date format is mm/dd/yy. If you want to change default date format then use dateformat property of datepicker function as,
  1. $(document).ready(function() {  
  2.       $("#EnterDate").datepicker({  
  3.   
  4.           dateFormat:"dd-mm-yy"  
  5.   
  6.       });  
  7.   });  
Now select date from calendar, It will be now dd-mm-yy as,
 
 
You can show Date and month dropdownlist on calendar control to quickly navigate between dates. To use this feature you need to use changeMonth and changeYear property in datepicker function as,
  1. $(document).ready(function() {  
  2.       $("#EnterDate").datepicker({  
  3.   
  4.           dateFormat:"dd-mm-yy",  
  5.      changeMonth :true,   
  6.     changeYear: true   
  7.   
  8.       });  
  9.   });  
Now run the application, then calendar will appear as follows,



From all the above examples, I hope you learned how to use jQuery UI calendar control in ASP.NET MVC.

Note:
  • For the complete code, please download the sample zip file.
  • Perform changes in Web.config file as per your server location.
  • You need to use the jQuery library.
Summary

I hope this article is useful for all readers. If you have a suggestion then please contact me.

Up Next
    Ebook Download
    View all
    Learn
    View all