2017-08-10 83 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" }); 

可能工作