Telerik Kendo 网格日期时间列始终为空

Telerik Kendo Grid DateTime column is always null

我有一个带有多个绑定列的 Telerik Kendo 网格。所有列都按预期工作,除了这个日期时间列 tsCreated,它 always null.

模特:

    [DataType(DataType.Date)]
    public DateTime tsCreated { get; set; }

网格:

@(Html.Kendo().Grid<NNC.ViewModels.OrderViewModel>()
        .Name("grid")
        .Columns(columns =>
        {
            columns.Bound(c => c.tsCreated);
            columns.Bound(c => c.orderNr);
            columns.Bound(c => c.customerName);
            columns.Bound(c => c.description);                
        })
        .Groupable()
        .Filterable()
        .Sortable()
        .Pageable()
        .DataSource(dataSource => dataSource
            .Ajax()
            .Events(events => events.Error("kendoGrid_ErrorHandler"))
            .Model(model => model.Id("orderNr"))
            .Read(read => read.Action("EditingInline_Read", "Orders"))                
        )
)

JSON 数据确实显示 tsCreated 中的日期:

{
 "Data":[{"orderNr":"13011155","tsCreated":"\/Date(1423579599377)\/",
 "description":"xxxx","customerName":"xxxxx"}],"Total":1,
 "AggregateResults":null,"Errors":null
 }

我还创建了一个 ClientTemplate 来显示值,如下所示:

columns.Bound(c => c.tsCreated).ClientTemplate(
                "W= #= tsCreated #"
            );

但它显示:

W= null

感谢任何帮助!

也许从 UNIX 纪元的日期转换失败了?可以尝试使用 Kendo UI 日期转换。

@(Html.Kendo().Grid<NNC.ViewModels.OrderViewModel>()
    .Name("grid")
    .Columns(columns =>
    {
        columns.Bound(c => c.tsCreated).ClientTemplate("W= #= kendo.toString(tsCreated , "m") #");
        columns.Bound(c => c.orderNr);
        columns.Bound(c => c.customerName);
        columns.Bound(c => c.description);                
    })
    .Groupable()
    .Filterable()
    .Sortable()
    .Pageable()
    .DataSource(dataSource => dataSource
        .Ajax()
        .Events(events => events.Error("kendoGrid_ErrorHandler"))
        .Model(model => model.Id("orderNr"))
        .Read(read => read.Action("EditingInline_Read", "Orders"))                
        )
)

如果您使用 Globalize.js,就会出现问题。 Kendo UI 与 Globalize 的日期函数冲突,因此目前的解决方法是在 Kendo UI 相关脚本之后加载 Globalize.js

以下是 Telerik 论坛上的相关讨论(位于:http://www.telerik.com/forums/registering-globalize-js-before-kendo-culture-js-causes-datetime-columns-in-grids-to-fail-formatting):

Basically when you load the globalize library, kendo.parseDate starts using Globalize.parseDate method instead of our own implementation of kendo.parseNumber. And since the globalize parser is not able to parse the format that the MVC returns the date successfully.

e.g.

Globalize.parseDate("/Date(-47876400000)/")

Loading the globalize library after the Kendo scripts wont override our implementation and the Grid will parse it successfully.

I am afraid there is no other work-around to this case.