我嘗試了幾乎所有在互聯網上閱讀的內容,在本網站上(web.config中的globlalization文化,在我的模型中添加的DisplayFormat ...),但沒有找到我的答案。MVC4無效的日期時間值
我的網絡應用程序允許用戶通過提供一些信息來創建人。其中一個信息是開始日期,它是一個DateTime值。我使用jQuery UI日期選擇器來允許通過日曆選擇日期。當我選擇我的日期(事實上,日期大於12的日期)時,應用程序向我顯示驗證錯誤消息。例如,如果我選擇今天的日期,則會顯示:值「03/15/2013」對於StartDate無效。
筆者認爲:
@model BuSIMaterial.Models.Person
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Person</legend>
<div class="editor-label">
First name :
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.FirstName, new { maxlength = 50 })
@Html.ValidationMessageFor(model => model.FirstName)
</div>
<div class="editor-label">
Last name :
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.LastName, new { maxlength = 50 })
@Html.ValidationMessageFor(model => model.LastName)
</div>
<div class="editor-label">
National number :
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.NumNat, new { maxlength = 11 })
@Html.ValidationMessageFor(model => model.NumNat)
</div>
<div class="editor-label">
Start date :
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.StartDate, new {@class = "datepicker" })
@Html.ValidationMessageFor(model => model.StartDate)
</div>
<div class="editor-label">
End date :
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.EndDate, new { @class = "datepicker" })
@Html.ValidationMessageFor(model => model.EndDate)
</div>
<div class="editor-label">
Distance House - Work (km) :
</div>
<div class="editor-field">
@Html.EditorFor(model => model.HouseToWorkKilometers)
@Html.ValidationMessageFor(model => model.HouseToWorkKilometers)
</div>
<div class="editor-label">
Category :
</div>
<div class="editor-field">
@Html.DropDownList("Id_ProductPackageCategory", "Choose one ...")
@Html.ValidationMessageFor(model => model.Id_ProductPackageCategory) <a href = "../ProductPackageCategory/Create">Add a new category?</a>
</div>
<div class="editor-label">
Upgrade? :
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Upgrade)
@Html.ValidationMessageFor(model => model.Upgrade)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/bundles/jqueryui")
@Styles.Render("~/Content/themes/base/css")
<script type="text/javascript">
$(document).ready(function() {
$(".datepicker").datepicker({
});
});
</script>
}
在我的角色模型的開始日期:
[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
[DataMemberAttribute()]
public global::System.DateTime StartDate
{
get
{
return _StartDate;
}
set
{
OnStartDateChanging(value);
ReportPropertyChanging("StartDate");
_StartDate = StructuralObject.SetValidValue(value);
ReportPropertyChanged("StartDate");
OnStartDateChanged();
}
}
private global::System.DateTime _StartDate;
partial void OnStartDateChanging(global::System.DateTime value);
partial void OnStartDateChanged();
沒有任何人有一個解決方案來處理這個問題呢?
不jQuery的datepicker返回一個字符串值?你如何解析它? – 2013-03-15 13:04:03
可能是您的代碼需要以mm/dd/yyyy格式顯示日期時間,其中日期選擇器以dd/mm/yyyy格式返回日期。所以15不是有效的月份數字,它是給出錯誤。 – Narendra 2013-03-15 13:04:14
@四十二:說實話,我遵循教程爲了實現日期選擇器功能,所以我沒有添加任何內容,但在教程 – Traffy 2013-03-15 13:09:07