本文介绍了为什么我的KendoGrid会“更新"?参数在控制器中始终为空吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Index.cshtml文件中包含以下代码:

I have the following code in my Index.cshtml file:

var dataSource = new kendo.data.DataSource({
    type: "json",
    transport: {
        read: {
            url: '@Url.Action("ReadTeachers", "EducationPortal")',
            dataType: "json"
        },
        update: {
            url: '@Url.Action("UpdateTeachers", "EducationPortal")',
            type: "POST"
        },
        parameterMap: function (data, operation) {
            if (operation != "read"){ 
                var result = {};

                for (var i = 0; i < data.models.length; i++) {
                    var teacher = data.models[i];
                    for (var member in teacher) {
                        result["teacher[" + i + "]." + member] = teacher[member];
                    }
                }

                return result;
            } else {
                return JSON.stringify(data);
            }
        }
    },
    batch: true,
    schema: {
        model: {
            id: "TeacherId",
            fields: {
                TeacherId: { type: "number" },
                FullName: { type: "string" },
                IsHeadmaster: { type: "boolean" }
            }
        }
    }
});

$("#teachers").kendoGrid({
    dataSource: dataSource,
    toolbar: ["create", "save"],
    columns: [
        { field: "FullName", title: "Teacher" },
        { field: "IsHeadmaster", title: "Is a Headmaster?", width: "120px" },
        { command: ["destroy"], title: "&nbsp;", width: "85px" }],
    editable: true
});

这是带有批处理"编辑的标准KendoGrid.编辑和网格本身都可以正常工作,但后端却不能.当更新"请求通过时,将转到此控制器方法:

This is a standard KendoGrid with "batch" editing. The editing and the grid itself both work fine, but the backend doesn't. When the "update" request goes through, it goes to this controller method:

public void UpdateTeachers(string models)
{
    // this method will have real code later
    Console.Write(models);
}

当我在此处放置断点时,Visual Studio显示modelsnull.像这样:

When I put a breakpoint in here, Visual Studio shows models to be null. Like so:

为什么是null?

推荐答案

在parameterMap中:function()

In your parameterMap: function ()

代替

return JSON.stringify(data);

使用

return { models: kendo.stringify(data) };

此处数据已序列化并附加到名称模型,现在该名称模型在您的操作中用作参数名称

here your data is serialize and attached to a name models now this name models is used as parameter name in your action

这篇关于为什么我的KendoGrid会“更新"?参数在控制器中始终为空吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 23:32