Typed text box with nullable DateTime

Jul 4, 2012 at 8:39 AM

Hi.

One of the recommended ways of handling nullable dates is, as I understand it, to use a typed text box.
I'm using the following code:

Model:

[Format(ClientFormat = "d")]
public DateTime? EndDate { get; set; }

View:

@Html.TypedTextBoxFor(m => m.EndDate, calendarOptions: new CalendarOptions { GoToCurrent=true})
What I want to achieve is that the user can enter a date if he wants to, but also leave it blank.
This works, except for client side validation. If I enter an invalid date, no validation message is displayed and the page is posted.
I would expect that if the text box contains something, it should be validated against the "ClientFormat".

Coordinator
Jul 4, 2012 at 12:34 PM

yes, if you have nullavle datetimes the reccomanded way to handle them is with this control.

While asp.net mvc adds automatcly client side rules for hill formed numbers,,,it doesnt do the same for datetime! However the Mvc Controls Toolkit has a validation provider that resolves this problem...However you have to install it in your global.asax. See here: http://mvccontrolstoolkit.codeplex.com/wikipage?title=Wrong%20Format%20Errors