[英]The field must be a date - DatePicker validation fails in mvc
I'm always getting the validation message like following when I submit the form to call post Action (Method) 提交表单以致电操作(方法)时,我总是收到如下验证消息
The field PatientDOB must be a date. PatientDOB字段必须为日期。
My model is 我的模特是
[Required(ErrorMessage = "Patient DOB is required")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
My view is like the following, 我的看法如下
@{
Layout = "~/Views/Shared/_FormLayout.cshtml";
var dob= Model.PatientDOB == DateTime.MinValue ? string.Empty : Model.PatientDOB.ToString("yyyy-MM-dd");
var phoneNo = Model.PatientPhoneNo == 0 ? string.Empty : Model.PatientPhoneNo.ToString();
}
<div class="row">
<div class="col-lg-2">
<label class="m-t-sm font-bold">Date of Birth:</label>
</div>
<div class="col-lg-2">
@Html.TextBoxFor(a => a.PatientDOB, "{0:yyyy-MM-dd}", new {@class = "bg-empty txtline input-s-sm", Value = @dob})
</div>
</div>
Note: I am using jquery datepicker for this fields jquery code 注意:我正在为此字段使用jQuery datepicker jQuery代码
<script type="text/javascript">
$(document).ready(function () {
$('#PatientDOB').datepicker({
dateFormat: "yy-mm-dd",
showStatus: true,
showWeeks: true,
currentText: 'Now',
autoSize: true,
gotoCurrent: true,
showAnim: 'blind',
highlightWeek: true
});
});
</script>
Earlier it was working fine have changed nothing in code but now showing this validation problem.I have tried solution given here solution tried.But none solved my problem. 以前它工作正常,但代码中什么都没改变,但是现在显示了这个验证问题。我尝试了给定解决方案的解决方案,但没有一个解决了我的问题。 stackoverflow-question
堆栈溢出问题
That's really old problem. 那真是老问题了。 The ultimate solution for it - use Globalize.js library and change jquery validation methods with globalize methods:
最终的解决方案-使用Globalize.js库,并使用globalize方法更改jquery验证方法:
$(document).ready(function () {
Globalize.culture("pl-PL"); //Not sure that it's your locale, maby you need other
$.validator.methods.date = function (value, element) {
return this.optional(element) || Globalize.parseDate(value);
};
});
You should init this script after jquery validation load. 您应该在jquery验证加载后初始化此脚本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.