[英]The field must be a date - DatePicker validation fails in mvc
提交表單以致電操作(方法)時,我總是收到如下驗證消息
PatientDOB字段必須為日期。
我的模特是
[Required(ErrorMessage = "Patient DOB is required")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
我的看法如下
@{
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>
注意:我正在為此字段使用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>
以前它工作正常,但代碼中什么都沒改變,但是現在顯示了這個驗證問題。我嘗試了給定解決方案的解決方案,但沒有一個解決了我的問題。 堆棧溢出問題
那真是老問題了。 最終的解決方案-使用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);
};
});
您應該在jquery驗證加載后初始化此腳本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.