[英]Confusion with Date Format asp.net mvc
I have a confusion with Date Format, in create mode, the validator accept only the format dd-MM-yyyy
instead of dd.MM.yyyy
. 我对日期格式感到困惑,在创建模式下,验证器只接受格式dd-MM-yyyy
而不是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. 在编辑模式下,它向我建议无效的格式dd.MM.yyyy
,所以我每次都要更改它。
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 :- 使用EditorFor()
而不是TextBoxFor()
为: -
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.