2015-03-31 122 views
-1

我在劍道網格彈出編輯器中有kendo datetimepicker。 我描述場模型格式:Kendo DateTimePicker在提交後更改格式

[DisplayFormat(DataFormatString = "{0:dd.MM.yyyy HH:mm:ss}", ApplyFormatInEditMode = true)] 

public DateTime Date { get; set; } 

而且在EditorTemplate

@Html.Kendo().DateTimePickerFor(model => model.Date).Value(DateTime.Now).Format("dd.MM.yyyy HH:mm") 

我也有DateTimeBinder

public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) 
    { 
     var displayFormat = bindingContext.ModelMetadata.DisplayFormatString; 
     var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);    

     if (!string.IsNullOrEmpty(displayFormat) && value != null && !String.IsNullOrWhiteSpace(value.AttemptedValue)) 
     { 
      DateTime date; 
      displayFormat = displayFormat.Replace("{0:", string.Empty).Replace("}", string.Empty); 

      if (DateTime.TryParseExact(value.AttemptedValue, displayFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out date)) 
      { 
       return date; 
      } 
      else 
      { 
       bindingContext.ModelState.AddModelError(bindingContext.ModelName, string.Format("{0} Incorrect Format", value.AttemptedValue)); 
      } 
     } 

     return base.BindModel(controllerContext, bindingContext); 
    } 

問題是,當我保存這個彈出,並達到了模型控制器服務器端它是格式dd.MM.yyyy H:mm:ss不是dd.MM.yyyy HH:mm:ss。例如,如果我保存這個時間31.03.2015 08:56,在服務器提交後,它變成31.03.2015 8:56。你有過這種情況嗎?

回答

0

您可以使用下面的方法,沒有任何問題。

型號:

[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)] 
[Display(Name = "Start Date")] 
public DateTime? StartDate{ get; set; } 

查看:

@Html.LabelFor(m => m.StartDate) 
@(Html.Kendo().DatePickerFor(m => m.StartDate) 
    .Animation(true) 
    .Culture("tr-TR") //Set culture 
    .Footer(false) 
    .Format("dd.MM.yyyy") 
    .Min(new DateTime(1900, 1, 1)) //Set min date of the datepicker 
    .Max(new DateTime(2099, 12, 31)) //Set min date of the datepicker 
    //.Value(DateTime.Today) //Set the value of the datepicker 
    //.HtmlAttributes(new { @class = "k-datepicker" }) 
) 

我覺得你並不需要 「DateTimeBinder」。希望這有助於...