2015-04-14 45 views
0

我一直在此停留了一個小時。DateTime動態字段無法正常工作

我有一個動態表單。添加新項目時,工作正常,

但後來我嘗試提交編輯表單我得到的客戶端驗證日期時間:

enter image description here

這是HTML:

using (Html.BeginCollectionItem("ValidDates")) 
{ 
    <div class="row mt-10"> 
     @Html.HiddenFor(m => m.Id) 
     @Html.HiddenFor(m => m.IsDeleted, new { data_is_deleted = "false" }) 
     <div class="col-md-5"> 
      @Html.TextBoxFor(m => m.ValidFrom, new { @Value = Model.ValidFrom.ToString("dd/MM/yyyy"), @class = "form-control dateTimePicker" }) 
      @Html.ValidationMessageFor(m => m.ValidFrom) 
     </div> 
     <div class="col-md-5"> 
      @Html.TextBoxFor(m => m.ValidTo, new { @Value = Model.ValidFrom.ToString("dd/MM/yyyy"), @class = "form-control dateTimePicker" }) 
      @Html.ValidationMessageFor(m => m.ValidTo) 
     </div> 
     <div class="col-md-1"> 
      @Html.CheckBoxFor(m => m.Enabled) 
     </div> 
     <div class="col-md-1"> 
      <span class="glyphicon glyphicon-trash" data-action="removeItem" title="remove" style="cursor:pointer"></span> 
     </div> 
    </div> 
} 

這是型號:

public class BucketValidDates : BaseEntity 
{ 
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] 
    public DateTime ValidFrom { get; set; } 

    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] 
    public DateTime ValidTo { get; set; } 

    public bool Enabled { get; set; } 

    public virtual Bucket Bucket { get; set; } 

    [NotMapped] 
    public bool IsDeleted { get; set; } 
} 

我甚至TR ied使用EditorFor(這工作時,我張貼回來但日期格式不正確)。

using (Html.BeginCollectionItem("ValidDates")) 
{ 
    <div class="row mt-10"> 
     @Html.HiddenFor(m => m.Id) 
     @Html.HiddenFor(m => m.IsDeleted, new { data_is_deleted = "false" }) 
     <div class="col-md-5"> 
      @Html.EditorFor(m => m.ValidFrom, new { @class = "form-control" }) 
      @Html.ValidationMessageFor(m => m.ValidFrom) 
     </div> 
     <div class="col-md-5"> 
      @Html.EditorFor(m => m.ValidTo, new { @class = "form-control" }) 
      @Html.ValidationMessageFor(m => m.ValidTo) 
     </div> 
     <div class="col-md-1"> 
      @Html.CheckBoxFor(m => m.Enabled) 
     </div> 
     <div class="col-md-1"> 
      <span class="glyphicon glyphicon-trash" data-action="removeItem" title="remove" style="cursor:pointer"></span> 
     </div> 
    </div> 
} 

我試圖改變格式模型{0:DD/MM/YYYY}

我需要的格式是:day/Month/Year 但你可以在圖片查看下面的格式爲Year/Month/day

enter image description here

的DatePicker的該項目是:http://eonasdan.github.io/bootstrap-datetimepicker/

這個約會是否與此有關?

+0

您當然應該在DisplayFormat設爲dd-MM-yyyy(除非有不同的原因)。 –

+0

@NathanKoop I h大道試圖將其設置爲dd-MM-yyyy,但我仍然得到相同的格式在查看 –

+0

檢查這篇文章http://stackoverflow.com/questions/18546971/mvc-4-how-to-validate-a-non- us-date-with-client-validation – ssimeonov

回答

0

我發現這似乎是現在的工作的解決方案:

public class BucketValidDates : BaseEntity 
    { 
     public DateTime ValidFrom { get; set; } 

     public DateTime ValidTo { get; set; } 

     public bool Enabled { get; set; } 

     public virtual Bucket Bucket { get; set; } 

     [NotMapped] 
     public bool IsDeleted { get; set; } 
    } 

HTML:

using (Html.BeginCollectionItem("ValidDates")) 
{ 
    <div class="row mt-10"> 
     @Html.HiddenFor(m => m.Id) 
     @Html.HiddenFor(m => m.IsDeleted, new { data_is_deleted = "false" }) 
     <div class="col-md-5"> 
      @Html.TextBoxFor(m => m.ValidFrom, new { @class = "form-control dateTimePicker", type = "text", data_val = "false" }) 
      @Html.ValidationMessageFor(m => m.ValidFrom) 
     </div> 
     <div class="col-md-5"> 
      @Html.TextBoxFor(m => m.ValidTo, new { @class = "form-control dateTimePicker", type = "text", data_val = "false" }) 
      @Html.ValidationMessageFor(m => m.ValidTo) 
     </div> 
     <div class="col-md-1"> 
      @Html.CheckBoxFor(m => m.Enabled) 
     </div> 
     <div class="col-md-1"> 
      <span class="glyphicon glyphicon-trash" data-action="removeItem" title="remove" style="cursor:pointer"></span> 
     </div> 
    </div> 
} 

jQuery來激活dateTimePicker的:

<script type="text/javascript"> 

    $(function() { 
     $('.dateTimePicker').datetimepicker({ 
      format: 'DD/MM/YYYY' 
     }); 
    }); 

</script> 
+1

這不適用於所有情況。如果客戶端文化使用不同的日期格式,jQuery驗證將再次返回錯誤。在你的情況下,它的作品,因爲你的電腦上的文化使用相同的日期格式。 – ssimeonov