Calendar Control Using Model Class in ASP.NET MVC

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:
 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:
using System;  
using System.ComponentModel.DataAnnotations;  
  
namespace UsingCalendarControlWithoutScriptInMVC.Models  
{  
    public class EmpModel  
    {  
        /// <summary>  
        /// DOB datetime data type property   
        /// to display date type control  
        /// </summary>  
        [Display(Name = "Date of Birth")]                 
        [DataType(DataType.Date)]  
        public DateTime ? DOB { get; set; }  
    }  
}  
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,
using System.Web.Mvc;  
  
namespace UsingCalendarControlWithoutScriptInMVC.Controllers  
{  
    public class HomeController : Controller  
    {  
        // GET: Home  
        public ActionResult Index()  
        {  
            return View();  
        }  
    }  
}  
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
@model UsingCalendarControlWithoutScriptInMVC.Models.EmpModel  
  
@{  
    ViewBag.Title = "www.compilemode.com";  
}  
@using (Html.BeginForm())  
{  
    @Html.AntiForgeryToken()  
  
    <div class="form-horizontal">  
  
        <hr />  
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })  
        <div class="form-group">  
            @Html.LabelFor(model => model.DOB, htmlAttributes: new { @class = "control-label col-md-2" })  
            <div class="col-md-10">  
                @Html.EditorFor(model => model.DOB, new { htmlAttributes = new { @class = "form-control" } })  
                @Html.ValidationMessageFor(model => model.DOB, "", new { @class = "text-danger" })  
            </div>  
        </div>  
  
  
    </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 in the 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.
  • 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 MVC:

Post a Comment

www.CodeNirvana.in

Protected by Copyscape
Copyright © Compilemode