I have two fields in my model
- CreateDateTo
- CreateDateFrom
which renders like this
<b>Start Date</b> @Html.EditorFor(model => model.AdvanceSearch.CreateDatefrom, new { @class = "picker startDate" })
<b>End Date</b> @Html.EditorFor(model => model.AdvanceSearch.CreateDateto, new { @class = "picker endDate" })
I have a validation scenario that enddate should not be greater then start date, currently I am validating it by jquery
$.validator.addMethod("endDate", function (value, element) {
var startDate = $('.startDate').val();
return Date.parse(startDate) <= Date.parse(value);
}, "* End date must be Equal/After start date");
I want to know that is there any way in MVC3 model validation to do this?
You need to create a custom validation against the model. You could put this in the controller after if(Model.IsValid)
But I would stick to javascript.
It works on the clientside and does not hit the server.
Unless you just need the added assurance.