This project is read-only.

Typed text box with nullable DateTime

Jul 4, 2012 at 8:39 AM


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:


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


@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".

Jul 4, 2012 at 12:34 PM

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

While 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: