Displaying Calender Control In ASP.NET MVC Without jQuery And JavaScript

Background

Many times we need to work with calendar control to fulfill business requirements but many times there is lots of scripting involved like jQuery, JavaScript etc. when you wants to work with calendar control .

MVC is a very powerful framework with a wide range of built in features and to display calendar control without jQuery and javascript we will use the Data Annotation. So let's see step by step so a beginner can also understand .

Step 1: Create an MVC Application.

Now let us start with a step by step approach from the creation of a simple MVC application as in the following:

  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", then provide the Project a name as you wish and click OK. After clicking, the following window will appear:



  3. As shown in the preceding screenshot, click on Empty template and check MVC option, then click OK. This will create an empty MVC web application whose Solution Explorer will look like the following:


Step 3: Create Model Class.

Now let us create the model class named EmpModel.cs by right clicking on model folder as in the following screenshot:



Note:

It is not mandatory that Model class should be in Model folder, it is just for better readability you can create this class anywhere in the solution explorer. This can be done by creating different folder name or without folder name or in a separate class library.

EmpModel.cs class code snippet:
  1. using System;  
  2. using System.ComponentModel.DataAnnotations;  
  3.   
  4. namespace UsingCalendarControlWithoutScriptInMVC.Models  
  5. {  
  6.     public class EmpModel  
  7.     {  
  8.         /// <summary>  
  9.         /// DOB datetime data type property   
  10.         /// to display date type control  
  11.         /// </summary>  
  12.         [Display(Name = "Date of Birth")]                 
  13.         [DataType(DataType.Date)]  
  14.         public DateTime ? DOB { getset; }  
  15.     }  

Step 4 : Create Controller.

Now let us add the MVC 5 controller as in the following screenshot:



After clicking on Add button it will show the window. specify the Controller name as Home with suffix Controller.

Note:

The controller name must be having suffix as 'Controller' after specifying the name of controller. Now the default code in HomeController.cs will look like as follows,
  1. using System.Web.Mvc;  
  2.   
  3. namespace UsingCalendarControlWithoutScriptInMVC.Controllers  
  4. {  
  5.     public class HomeController : Controller  
  6.     {  
  7.         // GET: Home  
  8.         public ActionResult Index()  
  9.         {  
  10.             return View();  
  11.         }  
  12.     }  

Don't change preceding code of HomeController as we are not going to add additional functionality.

Step 5 : Create View.

To create the View , right click on view folder and then click Add view. Now specify the view name as Index or as you wish, choose create scaffolding template and model class EmpModel.cs and click on Add button. 
 
After clicking on Add button it will create the Index view having extension .cshtml , Now the following default code is generated into the Index.cshtml as,
Index.cshtml
  1. @model UsingCalendarControlWithoutScriptInMVC.Models.EmpModel  
  2.   
  3. @{  
  4.     ViewBag.Title = "www.compilemode.com";  
  5. }  
  6. @using (Html.BeginForm())  
  7. {  
  8.     @Html.AntiForgeryToken()  
  9.   
  10.     <div class="form-horizontal">  
  11.   
  12.         <hr />  
  13.         @Html.ValidationSummary(true, "", new { @class = "text-danger" })  
  14.         <div class="form-group">  
  15.             @Html.LabelFor(model => model.DOB, htmlAttributes: new { @class = "control-label col-md-2" })  
  16.             <div class="col-md-10">  
  17.                 @Html.EditorFor(model => model.DOB, new { htmlAttributes = new { @class = "form-control" } })  
  18.                 @Html.ValidationMessageFor(model => model.DOB, "", new { @class = "text-danger" })  
  19.             </div>  
  20.         </div>  
  21.   
  22.   
  23.     </div>  

Step 6: Run the Application.

Now run the application the Index view appears as in the following screenshots. The Calendar UI differs as per browser.
  • Microsoft Edge: The calendar control in Microsoft Edge browser will look like as follows, 
 
The well designed calendar control will be displayed as shown inthe preceding image. Now choose the data , then the date will  look like as follows,
 
 
  • Google Chrome: The calendar control in Google Chrome browser will be look like as follows,
 
 
Now Display only month in calendar,
 
 
  • Opera: The calendar control in Opera  browser will look like as follows,
 
 
From the preceding examples we have learned how to display calendar control in  ASP.NET MVC without jQuery and javascript.

Note:
  • Data Annotation Date Data type calendar only support to those browser which are fully capable of HTML 5 date type .
  • IE and Firefox does not support to HTML 5 date type , So this example will not be work on IE and Firefox.
  • This Demo fully works on Google Chrome ,Microsoft Edge , Opera and Safari browser because the support HTML 5 date type.
  • You can customize calendar including date format as per your need.
  • Download the Zip file of the sample application for a better understanding.
  • Since this is a demo, it might not be using proper standards, so improve it depending on your skills.
  • This application is created completely focusing on beginners.
Summary

I hope this article is useful for all readers. If you have any suggestions please contact me.

Read more articles on ASP.NET:


Similar Articles