I have a model with a nullable DateTime property.
The date does not need to be entered, hence the nullable type. If it IS entered, though, it should be validated to lie forward in time.
Using Remote Validation on the property however, always gives null as an in-parameter in my validation method, even if the datepicker-field contains a value using the correct format. How can I get it to try the value from my input field?
The datepicker calender is limited to not be able to pick past dates. But you are still able to input a date manually. Doing so triggers the validation but always with a null value as in-parameter. I have a similar function where the DateTime is not nullable and it works correctly.
MODEL
[Remote("CheckDateNotInPast", "Validation", ErrorMessage = @"The date must lie forward in time.")]
public DateTime? Deadline { get; set; }
VALIDATION CONTROLLER
//The date parameter here is always null, even when the field contains a correct value to check.
public JsonResult CheckDateNotInPast(DateTime? date)
{
return Json((date == null || DateTime.UtcNow.Date <= date.Date), JsonRequestBehavior.AllowGet);
}
VIEW
<div class="col-sm-5">
@Html.TextBoxFor(m => m.Deadline, new { @placeholder = (Model.Deadline == null ? "YYYY-MM-DD" : Model.Deadline.Value.ToString("yyyy-MM-dd")), @class = "form-control", @id = "datepicker" })
<span class="nais-error-text">@Html.ValidationMessageFor(m => m.Deadline)</span>
</div>