This project is read-only.

DateTimeInput in editor template

Jul 4, 2012 at 12:06 PM
Edited Jul 4, 2012 at 12:08 PM

Hi.

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
	},
	false)

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.

Coordinator
Jul 4, 2012 at 12:44 PM

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:

Html.JQueryDatePickerGlobalizationScript()

See the doc here: http://mvccontrolstoolkit.codeplex.com/wikipage?title=DateTimeInput%20and%20DateRange%20attribute 

To learn more about globalization settings see here: http://mvccontrolstoolkit.codeplex.com/wikipage?title=Globalization

I will investigate the problem of the m => m

Jul 5, 2012 at 8: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.

Coordinator
Jul 5, 2012 at 10: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 see...is 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 template...it 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 TextBoxFor...so 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: http://mvccontrolstoolkit.codeplex.com/wikipage?title=Globalization 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:  

Html.JQueryDatePickerGlobalizationScript()

 

See also  the examples projects (bynariesWithSimpleExamples)