2017-08-10 84 views
0

我正在创建可以创建事件的日历。在使用jquery日期时间选择器时,不存在实时验证错误,但是当我按下create时,会出现验证错误,它说明了这一点;Asp.net MVC有效日期的DateTime错误

'我填写的日期'的值对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> 
} 

感谢您的帮助关于

+0

你发送的日期格式是什么?错误是指出C#不能解析它到一个DateTime –

+0

我认为这是与文化和datepicker问题有关。错误状态'DateTime'值无法解析,哪些值导致错误? –

+0

''我填写的日期'的值不适用于StartDateTime.'从字符串转换为日期时发生此错误 –

回答

0
// http://docs.jquery.com/Plugins/Validation/Methods/date 
date: function(value, element) { 
return this.optional(element) || !/Invalid|NaN/.test(new Date(value).toString());} 

把一个破发点上的脚本线1026(?) /jquery.validate.js 我将它编辑为:

date: function (value, element) { 
    return this.optional(element) || moment(value, "DD-MMM-YYYY").isValid(); 
    }, 

我做的另一件事是把DataAnnotations在POCO类: [DisplayFormat(ApplyFormatInEditMode =真,DataFormatString = “{0:DD-MMM-YYYY}”)]

$("input[type='datetime']").datepicker({ dateFormat: "dd-M-yy" }); 

可能工作