-1

我在剑道网格弹出编辑器中有剑道 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。你有过这样的情况吗??

4

1 回答 1

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”。希望这可以帮助...

于 2015-04-01T07:23:51.533 回答