MVC EditorTemplate for Date, DateTime, and Time

The Setup

If you do not understand what I mean by unintuitive, try creating the following MVC web application or skip to The Problem section below

1) Start a new project ASP.NET Web Application in Visual Studio

Visual Studio new ASP.NET Web Application dialog

2) Set MVC as the template type

Selecting MVC template in ASP.NET project wizard

3) Add an Entity Framework Model of a SQL database. For this article, it is a simple table that contains a few columns including a Date, DateTime and Time. You can generate the database from the Project files linked at the end of the article.

Entity Framework model with Date DateTime and Time columns

SQL table structure with date type columns

4) Scaffold a Controller and Views from this Model

Scaffolding Controller and Views from EF model

5) Alter Layout so that a link to this new Controller is added to the navigation menu

Adding navigation link in MVC Layout file

The Problem

Notice that when you run this MVC web application the Date, DateTime and Time columns from the model is shown as a basic textbox.

MVC form showing date fields as plain textboxes

The Solution, Editor Templates

1) Start by adding the JQuery UI and JQuery Timepicker Addon NuGet packages with the Package Manager Console

The commands are:

Install-Package jQuery.UI.Combined -Version 1.12.1
Install-Package jQuery-Timepicker-Addon -Version 1.6.3

2) Add a JavaScript file called site.js to the Scripts folder

3) Add this code to the site.js file

$(document).ready(function () {
    $(":input[date-picker]").datepicker({
        dateFormat: "yy/mm/dd",
    });
    $(":input[datetime-picker]").datetimepicker({
        dateFormat: "yy/mm/dd",
        timeFormat: "HH:mm:ss"
    });
    $(":input[time-picker]").timepicker({
        timeFormat: "HH:mm:ss"
    });
})

Essentially this JavaScript triggers when DOM is ready and applies the appropriate picker control based on the attribute set within the EditorTemplates in steps 6, 7 and 8

4) Add references to these new files to the _Layout.cshtml file located in the Views then Shared folder

5) Add a folder called EditorTemplates to the Shared folder under Views

6) Add a view called Date.cshtml to this new EditorTemplates folder and add this code

@model System.DateTime?

@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, new { date_picker = true, @class = "form-control", autocomplete = "off" })

This will add the date-picker attribute via the Editor Template model if the column is a DateTime

7) Add a view called DateTime.cshtml to this new EditorTemplates folder and add this code

@model System.DateTime?

@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, new { datetime_picker = true, @class = "form-control", autocomplete = "off" })

8) Add a view called TimeSpan.cshtml to this new EditorTemplates folder and add this code

@model System.TimeSpan?

@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, new { time_picker = true, @class = "form-control", autocomplete = "off" })

9) Adding a Date UIHint

You might notice that two Editor Templates exist for DateTime. Unfortunate C#, unlike MSSQL, does not have a Date type. Luckily we can nudge MVC to display it as a Date Editor Template with a UIHint

namespace TestMVC
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;

    public partial class Record
    {
        public System.Guid ID { get; set; }
        public string Name { get; set; }
        [UIHint("Date")]
        [DisplayFormat(DataFormatString = "{0:yyyy/MM/dd}", ApplyFormatInEditMode = true)]
        public System.DateTime Date { get; set; }
        [DisplayFormat(DataFormatString = "{0:yyyy/MM/dd HH:mm:ss}", ApplyFormatInEditMode = true)]
        public System.DateTime DateAndTime { get; set; }
        public System.TimeSpan Time { get; set; }
    }
}

The Result

The date columns have a date picker

MVC form with jQuery UI date picker control

The date and time columns have a date and time picker

MVC form with jQuery UI datetime picker control

The time columns have a time picker

MVC form with jQuery UI time picker control

You can download the project used within this article from https://bitbucket.org/svermaak/testmvc/downloads/

Leave a Reply