【发布时间】:2013-03-15 12:59:56
【问题描述】:
我尝试了我在互联网上阅读的几乎所有内容,在这个网站上(web.config 中的全球化文化,在我的模型中添加的 DisplayFormat,...)但我没有找到答案。
我的网络应用程序允许用户通过提供一些信息来创建人物。这些信息之一是开始日期,它是一个 DateTime 值。我正在使用 jQuery UI 日期选择器来允许通过日历选择日期。当我选择我的日期(实际上是一天大于 12 的日期)时,应用程序会向我显示一条验证错误消息。比如我选择今天的日期,会出现:The value '03/15/2013' is not valid for 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>
}
我的 Person 模型中的开始日期:
[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 的日期选择器不返回字符串值吗?你是怎么解析的?
-
可能是您的代码需要 mm/dd/yyyy 格式的日期时间,而日期选择器返回 dd/mm/yyyy 格式的日期。所以 15 不是一个有效的月份数,它给出了错误。
-
@Forty-Two :老实说,我遵循了一个教程来实现 datepicker 功能,所以除了教程中的代码之外我没有添加任何东西
-
您在控制器中收到此错误?
-
@Narendra :我也尝试过这个解决方案,但是应用程序显示“该字段必须是日期”作为验证错误消息。
标签: c# jquery asp.net-mvc