Confusion with asp.net mvc date format
I have some confusion with the date format, in create mode the validator only accepts the format dd-MM-yyyy
instead dd.MM.yyyy
. And in edit mode it offers me a format dd.MM.yyyy
which is not valid so I have to change it every time.
Here is the date property:
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd.MM.yyyy}", ApplyFormatInEditMode = true)]
public DateTime dateOut
{
get;
set;
}
Can someone help me find where the error is?
Thank.
+3
source to share
4 answers
I found this, works for me. It allows you to submit a form if the date is in the specified format:
@Html.TextBoxFor(m => m.Date, "{0:dd.MM.yyyy}")
<script type="text/javascript">
jQuery(function ($) {
$.validator.addMethod('date',
function (value, element) {
if (this.optional(element)) {
return true;
}
var ok = true;
try {
$.datepicker.parseDate('dd.mm.yy', value);
}
catch (err) {
ok = false;
}
return ok;
});
});
$("form").each(function () { $.data($(this)[0], 'validator', false); });
$.validator.unobtrusive.parse("form");
</script>
0
source to share