I have a confusion with Date Format, in create mode, the validator accept only the format dd-MM-yyyy
instead of dd.MM.yyyy
. And in edit mode it proposes to me the format dd.MM.yyyy
that is not valid so I have to change it each time.
Here is the date property:
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd.MM.yyyy}", ApplyFormatInEditMode = true)]
public DateTime dateOut
{
get;
set;
}
Could someone help me to find where is the error ?
Thank you.
The periods in the format specifies the date separator in the current culture. You can use literal periods instead:
[DisplayFormat(DataFormatString = "{0:dd'.'MM'.'yyyy}", ApplyFormatInEditMode = true)]
Use EditorFor()
instead of TextBoxFor()
as :-
View :-
@Html.EditorFor(m => m.dateOut)
Model :-
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd.MM.yyyy}", ApplyFormatInEditMode = true)]
public DateTime dateOut
{
get;
set;
}
尝试这个:
@Html.TextBoxFor(m => m.Date, "{0:dd.MM.yyyy}")
I found it, works for me. It's allows form to be send if date is in format that you have specified:
@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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.