我做了一個MVC應用程序,用戶選擇一個開始日期和結束日期。他們使用jQuery datepicker輸入這些數據。但是,當我點擊保存時,我收到驗證消息,說它不是驗證日期。 textbox
接受日期格式爲yyyy/mm/dd。不知道爲什麼我收到這些驗證消息,因爲我已經嘗試了很多東西。dd/mm/yyyy日期驗證MVC
我的代碼如下
Create.cshtml查看
<div class="form-group">
<script src="@Url.Content("~/Scripts/jquery-ui.min.js")" type="text/javascript"></script>
<link href="@Url.Content("/Content/jquery-ui.min.css")" rel="stylesheet" type="text/css" />
@Html.LabelFor(model => model.StartDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<input type="text" id="MyDate"
@Html.EditorFor(model => model.StartDate, "{0:dd/MM/yyyy}", new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.StartDate, "", new { @class = "text-danger" }) />
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.FinishDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<input type="text" id="FinishDate"
@Html.EditorFor(model => model.FinishDate, "{0:dd/MM/yyyy}", new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FinishDate, "", new { @class = "text-danger" }) />
</div>
</div>
<p>
<button class="btn btn-success"><span class="glyphicon glyphicon-plus"></span>Create</button>
</p>
</div>
}
<script type="text/javascript">
$(document).ready(function() {
$("#MyDate").datepicker();
$.validator.methods.date = function (value, element) {
Globalize.culture("en-AU");
// you can alternatively pass the culture to parseDate instead of
// setting the culture above, like so:
// parseDate(value, null, "en-AU")
return this.optional(element) || Globalize.parseDate(value) !== null;
}
});
</script>
<script type="text/javascript">
$(document).ready(function() {
$("#FinishDate").datepicker();
});
</script>
<div>
<a class=" btn btn-primary" href="@Url.Action("Index")"><span class=" glyphicon glyphicon-home"></span> Task List</a>
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
我也嘗試添加該到webconfig
<globalization culture="en-AU" uiCulture="en-AU" />
請添加您的視圖模型的代碼 - 我想你可以利用你的DisplayFormat屬性的還沒有準備好。 –
美國格式不是yyyy/mm/dd,而是mm/dd/yyyy。 –
@ErikFunkenbusch感謝您的意見,我一定會改變我的問題,但是您是否真的瞭解如何解決我的問題? –