DateTimeInput in editor template

Jul 4, 2012 at 11:06 AM
Edited Jul 4, 2012 at 11:08 AM


I'm trying to use the DateTimeInput in an editor template and having some problems.
This code won't work:

@model DateTime

@Html.DateTimeFor(m => m, DateTime.Now, dateInCalendar : true).DateCalendar(
	new CalendarOptions
		ShowOn = CalendarOptionsShowOn.button,
		ButtonImage = Url.Content("~/Content/images/calendar.gif"),
		ButtonImageOnly = true

It yields a "System.ArgumentNullException: Value cannot be null.", apparently due to the "m => m" construction (which works fine in e.g. Html.TextBoxFor).

Using "m => m.Date" instead works, but then client side validation doesn't work.

Jul 4, 2012 at 11:44 AM

mmmm this kind of control DO NOT NEED VALIDATION because errors should be corrected automaticly, or a wrong input shoule be blanked...The date part is handled by by the jQuery calendar....if you have format problems probably you have wrong globalization settings...since it uses the jQuery claendar you should include also the globalization settings fo the jQuery calendar:


See the doc here: 

To learn more about globalization settings see here:

I will investigate the problem of the m => m

Jul 5, 2012 at 7:08 AM

My scenario is that I want the text input initially empty (would be nice with support for this, now I have to do it by javascript).
Since the model value is required, if the text input is left empty you'd want the required validation message to be displayed, but it
isn't, probably due to the 'm => m' issue.

Jul 5, 2012 at 9:12 AM

The DateTimeFor CANNOT BE USED in EDTOR templates. The reason is that it is possible to define constraints amon several DateTime(see DateRange attribute here) of the same ViewModel. Now each DataTime For "looks" for other dateimes that migh be related with it in the same ViewModel....however if yiu put it in aneditor templates it is not able to do this since the only model it can itself(m => m)...It need to be part of an object so it can check the other DateTimes of the same object (the m of m=>m.StartDate   is the object it checks). Unluckly due to the way metadata are handled by Mvc   if you put the DateTimeFor in and eitor cannot access the original object it is in ....

"My scenario is that I want the text input initially empty "

If you want to handle empty dates please use the TypedTextBox with a datetime, it works like a it has validation and so on. If you need also Date Format validation you need to set up all javascript files for globalization, since date formats are tightly connected to a culture. See here: then format verification should work...however since the TypedTextBoxFor uses the jQuery calendar that "makes" some control on keys hit by the user it shouldn' be possible to insert wron formatted date easily...(maybe using copy and paste you can). However dont forget also the globalization script for the jQuery calendar:  



See also  the examples projects (bynariesWithSimpleExamples)