OK this is not the classic DateTime picker bound to a textbox… with a jQuery calendar😉.
If you need a custom datetime editor template that splits the datetime parts in drop-down lists like this…

<%= Html.EditorFor(model => model.BirthDate, “Date”) %>

…or like this…

<%= Html.EditorFor(model => model.EventDateTime, “DateTime”) %>

…then this post may help you. As you should know, in ASP.NET MVC 2 the default model binder has some difficulties to combine splitted datetime parts in the View. So, if you need to define a DateTime property in your model and make a custom editor template that splits the DateTime parts in different controls (e.g. TextBox and/or DropDownList), first you should read this smart solution by Scott Hanselman. The idea is to separate the way we render the month field, the day field, the year field etc. from the mechanism that will assemble them back in a DateTime structure for model binding.
Starting from the Global.asax, the first thing to do is to register the Scott’s Custom Model Binder and then specify all the available options (the strings there are the suffixes of the fields in your View that will be holding the Date, the Time, the Day etc.)

ModelBinders.Binders[typeof(DateTime)] = newDateTimeModelBinder()
{
Date = "Date", // Date parts are not splitted in the View
// (e.g. the whole date is held by a TextBox with id “xxx_Date”)
Time = "Time", // Time parts are not splitted in the View
// (e.g. the whole time is held by a TextBox with id “xxx_Time”) Day = "Day",
Month = "Month",
Year = "Year",
Hour = "Hour",
Minute = "Minute",
Second = "Second"};

Now, let’s have a look to template editors. In Views\Shared\EditorTemplates directory we can put two simple templates: Date.ascx and DateTime.ascx. The former renders only the drop-down lists for the date part of the DateTime structure (Month, Day, Year), while the latter renders the time part too. Here the code for Date.ascx:

That’s all!
Note that in the template editor above, the Hour, Minute and Second parts are rendered as HTML hidden fileds, because the Scott’s DateTimeModelBinder configured in the Global.asax expects a value for all the six parts of the splitted DateTime structure. It’s just a clean workaround to make the Scott’s model binder work without any change to the original code. In a real implementation hidden fields should be not required😉.

Now, what about validation? Well, both client-side and server-side validations are quite trivial: the server-side validation can be obtained through a custom ValidationAttributethat checks if the DateTime value is correct (e.g. the value should be not equal to DateTime.MinValue or DateTime.MaxValue).

The corresponding client-side validation adapter can be implemented by deriving the DataAnnotationsModelValidator class. It allows us to specify a remote validation rule from the client. In this scenario, the part of the DateTime structure that could be validated is the Date part.
So, we can create a SplittedDateRequiredValidator in order to check if each drop-down is holding a valid value. To accomplish this requirement, a simple solution is to make the client-side validator aware of the IDs of the <select> elements holding the DateTime’s Month, Day and Year values.

Before looking at javascript validator code, let’s register the SplittedDateRequiredValidator as the client-side validation adapter for all model properties decorated with the DateRequiredAttribute. To accomplish that, we have to put the following line of code in the Global.asax…

Finally, the client-side validator will evaluate the selected index of the drop-down lists in order to ensure that the user has selected a valid date (note that the isValidDate function simply checks if the users has specified an existing date).

I’m trying to utilize this is very useful control in ASP.net MVC using razor. In the JavaScript code you reference metadata.PropertyName + “_Day”, but nowhere in the aspx is an ID set on the control. How’s the ID supposed to get set?

Hi Stefan,
Well, the expression metadata.PropertyName + "_Day" represents the id of the client-side HTML drop down that holds the day part of the DateTime (in my example, the output HTML is ). The client-side model validator uses this id (passed as validation parameter) to find the right drop down in the DOM.
No id must be set explicitly in the aspx view, because it will be auto-generated at rendering time, according to the model binder specifications.

Johnston Mwakazisaid

anoopsaid

Thanks for the post, i am trying to resolve an issue with the validation error.

When the validation is fired, it is not adding the default “input-validation-error” css-class, to highlight the error.
Can you please suggest what should be done get this working on both client & server validation?

I’m finding that GetClientValidationRules is only called if I use TextBoxFor(datetime => datetime, … ie. adding a suffix on the end means that the validation attributes are not rendered. This is with MVC4 using IClientValidatable interface on the DateRequiredAttribute instead of creating and registering the separate validator object.

Faisalsaid

I am using a Model class in which one of the fields is of DateTime type.
How do you enforce this field to use Hanselman’s custom DateAndTimeModelBinder
The attribute to decorate a model to use a certain modelbinder is [ModelBinder(typeof())] and this is at class declaration level.

Faisalsaid

I realise that this article was written for MVC2 but it pretty much zeroes in on a requirement that I have. I have been trying to re-write this for an MVC5 project with less than full success in terms of functionality and best practice.
I had trouble getting the “data-” attributes to render on the select tags. So I had to resort to brute-forcing them into the template

the resulting attribute is data-val-splitdate=”Invalid date value” , which hooks the markup to the jquery unobtrusive callback.

I have written the script which only partially works, I can’t get options.message to display when the date is in invalid state.
Would really appreciate a review of this from anyone who has developed this solution for an MVC4/5 solution.