MVC 4日期格式化无法识别输入掩码(MVC 4 date formatting not recognizing input mask)

编程入门 行业动态 更新时间:2024-10-27 22:32:58
MVC 4日期格式化无法识别输入掩码(MVC 4 date formatting not recognizing input mask)

我遇到的问题是格式化为dd-MMM-yyyy的日期未被识别为日期。 我收到错误“字段开始日期必须是日期”。 对于使用datepicker 30-NOV-2013挑选的日期。

模型

[Required] [DataType(DataType.Date)] [Display(Name = "Start date")] [DisplayFormat(DataFormatString = "{0:dd-MMM-yyyy}", ApplyFormatInEditMode = true)] public DateTime StartDate { get; set; }

查看@ Html.ValidationSummary()

@Html.LabelFor(m => m.StartDate) @Html.EditorFor(m => m.StartDate) @Html.ValidationMessageFor(m => m.StartDate) <div> <input type="submit" value="Set on leave" id="setLeave" /> </div> <script type="text/javascript"> $("#StartDate").datepicker({ showAnim: 'fadeIn', dateFormat: 'dd-M-yy', changeMonth: true, changeYear: true, showButtonPanel: true }).prop('readOnly', true); </script>

I am having an issue where a date formatted as dd-MMM-yyyy is not being recognized as a date. I get the error "The field Start date must be a date." for a date picked with the datepicker 30-NOV-2013.

Model

[Required] [DataType(DataType.Date)] [Display(Name = "Start date")] [DisplayFormat(DataFormatString = "{0:dd-MMM-yyyy}", ApplyFormatInEditMode = true)] public DateTime StartDate { get; set; }

View @Html.ValidationSummary()

@Html.LabelFor(m => m.StartDate) @Html.EditorFor(m => m.StartDate) @Html.ValidationMessageFor(m => m.StartDate) <div> <input type="submit" value="Set on leave" id="setLeave" /> </div> <script type="text/javascript"> $("#StartDate").datepicker({ showAnim: 'fadeIn', dateFormat: 'dd-M-yy', changeMonth: true, changeYear: true, showButtonPanel: true }).prop('readOnly', true); </script>

最满意答案

我建议使用jQuery Globalize并自定义日期验证:

$(document).ready(function () { Globalize.culture('en-GB'); $.validator.methods.date = function (value, element) { return (this.optional(element) || Globalize.parseDate(value, 'dd-MMM-yyyy')); } });

您可以在此处阅读有关jQuery Globalize的更多信息。

I would suggest to use jQuery Globalize and customize dates validation:

$(document).ready(function () { Globalize.culture('en-GB'); $.validator.methods.date = function (value, element) { return (this.optional(element) || Globalize.parseDate(value, 'dd-MMM-yyyy')); } });

You can read something more about jQuery Globalize here.

更多推荐

本文发布于:2023-08-07 13:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464783.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:掩码   无法识别   日期   MVC   date

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!