The reason for the client side validation error is that you have generated a textbox for the property and by default, jquery.validate.js
validates dates based on a MM/dd/yyyy
format, whereas your format is dd/MM/yyyy
. You could override the $.validator
as explained in the answer to your subsequent question.
However the real issue is that you should not be generating a form control for a 'Create Date' (or 'Modified Date') property. Those values should only be set in the controllers POST method immediately before you save the object.
In the view, if your editing an existing object, use
<div>@Html.DisplayFor(m => m.CREATED)</div>
to display the date, and you can format the div to look like your other textboxes if necessary. You should also delete the associated ValidationMessageFor()
and LabelFor()
(you no longer have an associated form control so a <label>
element is not appropriate - you can just use (say) <span>@Html.DisplayNameFor(m => m.CREATED)</span>
)
If you wanted to submit the value of CREATED
, then you can also include a hidden input for the value (by default, hidden inputs are not validated)
@Html.HiddenFor(m => m.CREATED) // you can use a [DisplayAttribute] for formatting the value
However, because you editing data, you should be using a view model, not your data model. In the POST method, you get the original data model from the repository based on its ID, and update its properties based on the values from the view model (making the need for the hidden input redundant because you do not need to update that value). This pattern also protects you from malicious users who may alter the request and send back changed values for the dates.