I have a field declared in my model:
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime? dataCreazione { get; set; }
in the view i render the field with helper:
@Html.EditorFor(m => m.dataCreazione )
And when i submit the form with dataCreazione null (via js - form serialize), with the debug i see the dataCreazione field value null (correct) in controller method, but ModelState is invalid with message ("dataCreazione should be formated .....").
Why validation on nullable DateTime? How i can avoid it? I wanna be able to submit DateTime null without make modelstate invalid.
Edit: I submit form via js with:
$.post(this.form.attr('action'), this.form.serialize(), function (data) {
if (data.url == null) {
_this.modal.find('.modal-content').empty();
_this.modal.find('.modal-content').html(data);
_this.bindEvents();
} else {
window.location.href = data.url;
}
});
If i print form.serialize() i see parameter will be send dataCreazione=. In another point of application i submit a form with date null via Http GET and on server side the datetime is initialized with 01/01/0001 (and modelstate valid).
I just not complain why i can't submit a datetime null and receive a modelstate invalid.