Kendo网格以错误的格式发送日期

时间:2015-01-29 21:22:59

标签: asp.net-mvc asp.net-mvc-4 kendo-ui kendo-grid

是kendoui网格对抗webapi(.net mvc 4项目) 我的js文件中的kendoGrid部分:

$("#eventsgrid").kendoGrid({
        dataSource: {
            transport: {
                read: { url: "/Webapi/V2/.../events", type: "GET" },
                update: { url: "/Webapi/V2/.../events", type: "PUT" },
                create: { url: "/Webapi/V2/.../events", type: "POST" },
                destroy: { url: "/Webapi/V2/.../events", type: "DELETE" }
            },
            pageSize: 10,
            schema: {
                model: {
                    id: "eventId",
                    fields: {
                        eventId: { type: "number", editable: false, nullable: true },
                        eventCode: { type: "string" },
                        eventLocation: { type: "string" },
                        clientId:{ type: "string" },
                        startDate:{ type: "date" },
                        endDate: { type: "date" }
                    }
                }
            }
        },
        columns: [
            { field: 'eventId', title: 'ID', width: '50px', filterable: true },
            { field: 'eventCode', title: 'Code', width: '80px', filterable: true },
            { field: 'eventLocation', title: 'Location', width: '150px', filterable: true },
            { field: 'clientId', title: 'Client', width: '80px', filterable: true },
            { field: 'startDate', title: 'Start', width: '80px', format: "{0:MM/dd/yyyy}", filterable: { ui: "datepicker" } },
            { field: 'endDate', title: 'End', width: '80px', format: "{0:MM/dd/yyyy}", filterable: { ui: "datepicker" } },
            { command: [{ id: "edit", name: "edit", text: "Edit" }, { id: "destroy", name: "destroy", text: "Delete", width: "30px" }, { text: 'Details', click: gotouser }], title: "&nbsp", width: "240px" }
        ],
        sortable: true,
        editable: "popup",
        filterable: {
            extra: false,
            operators: {
                string: {
                    startswith: "Starts with",
                    eq: "Is equal to",
                    neq: "Is not equal to"
                }
            }
        },
        toolbar: [{ name: "create", text: "Add new Event" }],
        pageable: { pageSizes: true | [10, 20, 30] }

    });

问题是:当我编辑记录或发布新记录时,我在mvc控制器中收到的任何日期字段的值为null。 当我检查我的chrome工具时,看看网格发送给控制器的内容(当你在弹出窗口中点击更新时),如下所示:

    clientId: 1
    startDate: Tue Jan 20 2015 00:00:00 GMT-0500 (Eastern Standard Time)
    endDate: Thu Jan 22 2015 00:00:00 GMT-0500 (Eastern Standard Time)
    logo: abcsd.jpg
    featured: 4

显然日期格式是错误的,我想这就是为什么控制器没有把它看作日期,转换/绑定失败,它给了我null。

如何让它发送不同的格式,例如mm / dd / yyyy?是不是列数组中的格式定义所涵盖?

3 个答案:

答案 0 :(得分:1)

您可以尝试使用parameterMap功能。请看kendo parameterMap documentation

与此类似:

parameterMap: function (data, operation) {
    if (operation != "read") {
        var parsedDate = kendo.parseDate(data.startDate, "MM/dd/yyyy");;
        data.startDate = parsedDate;
        return data;
    }
}

对于kendo解析日期,请查看kendo parseDate

答案 1 :(得分:1)

在parameterMap中使用:

var parsedDate = kendo.toString(data.startDate, "MM/dd/yyyy")

kendo.parseDate函数将字符串作为参数

答案 2 :(得分:0)

好的,我跟着Dmitriy的路线,并且能够使它工作。所以对于后代,这里是你需要添加到你的js:

update: {
                    url: "/Webapi/..../events",
                    dataType: "json",
                    type: "PUT",
                    data: function (data) {
                        data.startDate = kendo.toString(data.startDate, "yyyy-MM-dd");
                        data.endDate = kendo.toString(data.endDate, "yyyy-MM-dd");
                        // repeat for all your date fields
                        return data;
                    }
                },
// same thing for your 'create'. 
// Sorry, look like a bit of 'Repeat yourself', but at least it's working.

再次,非常感谢Dimitriy!