0

使用带有弹出编辑的剑道网格。我验证了数据是从视图中发布的(我可以在“网络”选项卡中看到它,下面是它:

{"LetterId":12,"BodyText":"This is a test","CreatedDate":"07/31/2013","CreatedBy":"Grace Rodgers","ModifiedDate":"07/31/2013","ModifiedBy":"Grace Rodgers","PersonId":18,"FirstName":"Jason","LastName":"Bigby"}:

但是,我在控制器中的 json 方法上有一个断点,当将鼠标悬停在模型参数上时,显示所有字段都为空。这是控制器代码的前几行:

    [HttpPost]
    public JsonResult JsonEditLetter(LetterViewModel model)
    {

和视图中的剑道代码:

var PersId = $("#PersonId").val();

    var ds_LettersGrid = new kendo.data.DataSource({
        transport: {
            read: {
                url: '@Url.Action("JsonGetLetterList", "Letter")/' + PersId,
                dataType: 'json'
            },
            update: {
                url: '@Url.Action("JsonEditLetter", "Letter")',
                dataType: 'json',
                type: "POST"
            },
            parameterMap: function (data, type) {
                if (type == "update") {

                    data.models[0].CreatedDate = kendo.toString(new Date(data.models[0].CreatedDate), "MM/dd/yyyy");
                    data.models[0].ModifiedDate = kendo.toString(new Date(data.models[0].ModifiedDate), "MM/dd/yyyy");

              return kendo.stringify(data.models[0]);
                }
            },

        },
        batch: true,
        schema: {
            model: {
                id: "LetterId",
                fields: {
                    BodyText: { editable: true },
                    CreatedDate: { editable: false, type: "date"}, 
                    ModifiedDate: { editable: false, type: "date" },
                    CreatedBy: { editable: false},
                    ModifiedBy: { editable: false },
                    PersonId: { defaultValue: PersId }
                }
            }
        },
        pageSize: 10
    });

    $(document).ready(function () {

        $("#letter-list").kendoGrid({
            dataSource: ds_LettersGrid,
            sortable: true,
            filterable: { extra: false, operators: {
                string: { startswith: "Starts with", eq: "Is equal to" }
            }
            },
            pageable: true,
            columns: [{
                field: "BodyText", title: "Letter Content", width: 400, filterable: false
            }, {
                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: "", width: "110px"
            }],
            height: "300px",
            resizable: true,
            editable: "popup"
        });
    });
4

2 回答 2

2

您需要为您的 id 字段添加默认值,因为客户端生成新的 id 值,服务器自动增量生成的 id 值及其拍摄错误中已有的

schema: {
            model: {
                id: "LetterId",
                fields: {
                    LetterId: {defaultValue: 16000}
                    BodyText: { editable: true },
                    CreatedDate: { editable: false, type: "date"}, 
                    ModifiedDate: { editable: false, type: "date" },
                    CreatedBy: { editable: false},
                    ModifiedBy: { editable: false },
                    PersonId: { defaultValue: PersId }
                }
            }
        }
于 2013-08-01T22:15:26.567 回答
1

我想通了,它需要一个特定的内容类型。传递的类型是表单,但控制器需要 json。所以运输现在看起来像:

            update: {
                url: '@Url.Action("JsonEditLetter", "Letter")',
                dataType: 'json',
     >>>>>>>>   contentType: "application/json",
                type: "POST"
            },
于 2013-08-02T14:45:53.777 回答