【问题标题】:Kendo DateTimePicker changes format after submittingKendo DateTimePicker 提交后更改格式
【发布时间】:2015-03-31 09:55:28
【问题描述】:

我在剑道网格弹出编辑器中有剑道日期时间选择器。 我用格式描述模型中的字段:

[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。你有过这样的案例吗?

【问题讨论】:

    标签: .net asp.net-mvc date kendo-ui kendo-datetimepicker


    【解决方案1】:

    您可以使用下面的这种方法,没有任何问题。

    型号:

    [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”。希望这会有所帮助...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-08
      • 2018-03-29
      相关资源
      最近更新 更多