0

使用带有弹出创建的剑道网​​格。这是带有数据源的代码:

var PersId = $("#PersonId").val();
var ds_CommentsGrid = new kendo.data.DataSource({
    transport: {
        read: {
            url: '@Url.Action("JsonGetComments", "TrespassOrder")/' + PersId,
            dataType: 'json',
        },
        update: {
            url: '@Url.Action("JsonEditComment", "TrespassOrder")',
            dataType: 'json',
            type: "POST"
        },
        create: {
            url: '@Url.Action("JsonAddComment", "TrespassOrder")',
            dataType: 'json',
            type: "POST"
            //contentType: 'application/json; charset=UTF-8',
        },
        parameterMap: function (options, operation) {
            if (operation !== "read" && options.models) {
            var values = {};
            values["CommentText"] = options.models[0].CommentText;
            values["ModifiedBy"] = options.models[0].ModifiedBy;
            values["ModifiedDate"] = options.models[0].ModifiedDate;
            values["CreatedBy"] = options.models[0].CreatedBy;
            values["CreatedDate"] = options.models[0].CreatedDate;
            values["PersonId"] = options.models[0].PersonId;
            return values;
        }
        }
    },
    batch: true,
    schema: {
        model: {
            id: "CommentId",
            fields: {
                CommentText: { editable: true },
                CreatedDate: { editable: false , type: "date"}, 
                ModifiedDate: { editable: false , type: "date" },
                CreatedBy: { editable: false },
                ModifiedBy: { editable: false },
                PersonId: { editable: false}
            }
        }
    },
    pageSize: 5
});

$(document).ready(function () {

    $("#comment-list").kendoGrid({
        dataSource: ds_CommentsGrid,
        sortable: true,
        filterable: { extra: false, operators: {
            string: { startswith: "Starts with", eq: "Is equal to" }
        }
        },
        pageable: true,
        columns: [{
            field: "CommentText", title: "Comment", width: 300, filterable: true
        }, {
            field: "CreatedBy", title: "Author", filterable: false
        }, {
            field: "CreatedDate", title: "Original Date", format: "{0:g}", filterable: { ui: "datetimepicker" }
        }, {
            field: "ModifiedBy", title: "Edited By", filterable: false
        }, {
            field: "ModifiedDate", title: "Editted On", format: "{0:g}", filterable: { ui: "datetimepicker" }
            }, {
            command: ["edit"], title: "Actions"
        }],
        editable: "popup",
        toolbar: [{ name: "create", text: "Add New Comment" }]
    });
});

有 2 个问题: 1. PersonId 没有与 Create 一起发送。2. 日期以最终到达 MVC 控制器的格式发送为 null (1/1/0001)。

这是发送到控制器的内容:

Request URL:http://localhost:47621/TrespassOrder/JsonAddComment
Request Headersview source
Accept:application/json, text/javascript, */*; q=0.01
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Origin:http://localhost:47621
Referer:http://localhost:47621/Person/Detail/18
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36
X-Requested-With:XMLHttpRequest
Form Dataview sourceview URL encoded
    CommentText:Blah blah, I am a comment.
    ModifiedBy:
    ModifiedDate:Fri Jun 14 2013 12:12:46 GMT-0700 (Pacific Daylight Time)
    CreatedBy:
    CreatedDate:Fri Jun 14 2013 12:12:46 GMT-0700 (Pacific Daylight Time)
    PersonId:

注意 PersonId 是空的。

请注意传输中创建中已注释掉的 contentType。我尝试使用 json 内容类型,但返回错误说“CommentText 是无效的 JSON 原语”。

那么如何格式化日期以便它们显示在控制器中,以及如何将外键(PersonId)附加到发送的数据中?

4

1 回答 1

1

PersonId设置为不可编辑,它没有默认值,也没有column定义。您希望在创作时发送PersId什么?如果是这样,您可以defaultValueschema.model.fields.PersonIdas 中PersId,例如:

schema   : {
    model: {
        id    : "CommentId",
        fields: {
            CommentText : { editable: true },
            CreatedDate : { editable: false, type: "date"},
            ModifiedDate: { editable: false, type: "date" },
            CreatedBy   : { editable: false },
            ModifiedBy  : { editable: false },
            PersonId    : { editable: false, defaultValue: PersId}
        }
    }
},

关于传输日期的格式,它们是按原样传输的,strings因此您应该将它们转换为您的控制器能够解析的格式。为此,您应该使用kendo.toString(请参阅有关它的这篇文章)。您可以尝试使用Universal sortable date/time,例如:

parameterMap: function (options, operation) {
    if (operation !== "read" && options.models) {
        var values = {};
        values["CommentText"] = options.models[0].CommentText;
        values["ModifiedBy"] = options.models[0].ModifiedBy;
        values["ModifiedDate"] = kendo.toString(options.models[0].ModifiedDate, "u");
        values["CreatedBy"] = options.models[0].CreatedBy;
        values["CreatedDate"] = kendo.toString(options.models[0].CreatedDate, "u");
        values["PersonId"] = options.models[0].PersonId;
        return values;
    }
}
于 2013-06-14T21:30:21.330 回答