有效日期的 Asp.net MVC DateTime 错误
Posted
技术标签:
【中文标题】有效日期的 Asp.net MVC DateTime 错误【英文标题】:Asp.net MVC DateTime error on valid dates 【发布时间】:2018-01-18 11:16:14 【问题描述】:我正在制作一个可以创建活动的日历。使用 jquery 日期时间选择器时,没有实时验证错误,但是当我按下创建时,会出现验证错误,它指出;
值“我填写的日期”对于 StartDateTime 无效。
我认为在我按下创建后它会尝试以不同的格式进行验证。我不知道是哪一个以及如何检查。
这里有与创建它相关的代码。
事件模型:
public class Event
public int Id get; set;
public string Subject get; set;
public string Description get; set;
public DateTime StartDateTime get; set;
public DateTime EndDateTime get; set;
public string ThemeColor get; set;
public bool IsFullDay get; set;
创建页面:
@model BudoschoolTonNeuhaus.Models.Event
@
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Admin_Layout.cshtml";
<div class="container">
<h2>Create</h2>
@using (Html.BeginForm())
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Event</h4>
<hr />
@Html.ValidationSummary(true, "", new @class = "text-danger" )
<div class="form-group">
@Html.LabelFor(model => model.Subject, htmlAttributes: new @class = "control-label col-md-2" )
<div class="col-md-10">
@Html.EditorFor(model => model.Subject, new htmlAttributes = new @class = "form-control" )
@Html.ValidationMessageFor(model => model.Subject, "", new @class = "text-danger" )
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Description, htmlAttributes: new @class = "control-label col-md-2" )
<div class="col-md-10">
@Html.EditorFor(model => model.Description, new htmlAttributes = new @class = "form-control" )
@Html.ValidationMessageFor(model => model.Description, "", new @class = "text-danger" )
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.StartDateTime, htmlAttributes: new @class = "control-label col-md-2" )
<div class="col-md-10">
@Html.EditorFor(model => model.StartDateTime, new htmlAttributes = new @class = "form-control datepicker" )
@Html.ValidationMessageFor(model => model.StartDateTime, "", new @class = "text-danger" )
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.EndDateTime, htmlAttributes: new @class = "control-label col-md-2" )
<div class="col-md-10">
@Html.EditorFor(model => model.EndDateTime, new htmlAttributes = new @class = "form-control datepicker" )
@Html.ValidationMessageFor(model => model.EndDateTime, "", new @class = "text-danger" )
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ThemeColor, htmlAttributes: new @class = "control-label col-md-2" )
<div class="col-md-10">
@Html.EditorFor(model => model.ThemeColor, new htmlAttributes = new @class = "form-control" )
@Html.ValidationMessageFor(model => model.ThemeColor, "", new @class = "text-danger" )
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.IsFullDay, htmlAttributes: new @class = "control-label col-md-2" )
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.IsFullDay)
@Html.ValidationMessageFor(model => model.IsFullDay, "", new @class = "text-danger" )
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
<div>
@Html.ActionLink("Back to List", "AdminIndex")
</div>
</div>
@section Scripts
<script>
$('.datepicker').datepicker( );
</script>
感谢您的帮助
【问题讨论】:
您发送的日期是什么格式?该错误指出 C# 无法将其解析为 DateTime 我认为这与文化和日期选择器问题有关。错误状态DateTime
value can't beparsed,哪些值导致错误?
The value 'the date i filled in' is not valid for StartDateTime.
从字符串转换为日期时间时出现此错误
@RoryMcCrossan 我发送的格式是 mm-dd-yyyy
@Diceble 默认情况下,您的站点文化遵循您机器上的区域设置配置。如果文化不同于 datepicker 使用的默认文化,那么您需要使用自定义输入格式并覆盖默认验证模式。
【参考方案1】:
// http://docs.jquery.com/Plugins/Validation/Methods/date
date: function( value, element )
return this.optional(element) || !/Invalid|NaN/.test(new Date(value).toString());
在 scripts/jquery.validate.js 的第 1026 行 (?) 放置一个断点 我将其编辑为:
date: function (value, element)
return this.optional(element) || moment(value, "DD-MMM-YYYY").isValid();
,
我做的另一件事是将 DataAnnotations 放在 POCO 类中: [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "0:dd-MMM-yyyy")]
$("input[type='datetime']").datepicker( dateFormat: "dd-M-yy" );
可能有用
【讨论】:
以上是关于有效日期的 Asp.net MVC DateTime 错误的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 JSON.NET 通过 ASP.NET MVC 传递 JSON 日期值? [复制]
正确格式化 AngularJS 的 ASP.NET MVC 日期