为什么我的 KendoGrid 不调用我的 MVC 控制器? [英] Why doesn't my KendoGrid call my MVC controller?

查看:24
本文介绍了为什么我的 KendoGrid 不调用我的 MVC 控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在标准 C# ASP.NET MVC 控制器中有以下代码.

I have the following code in a standard C# ASP.NET MVC controller.

public JsonResult ReadTeachers()
{
    return Json(ReadTeacherData(), JsonRequestBehavior.AllowGet);
}

public void UpdateTeachers(IEnumerable<Teacher> updatedTeachers)
{
    // this is never called
}

我正在尝试使用 KendoGrid 调用此控制器.这是我的网格的代码.

I'm trying to call this controller with a KendoGrid. Here is the code for my grid.

$("#teachers").kendoGrid({
    dataSource: {
        type: "json",
        transport: {
            read: {
                url: '@Url.Action("ReadTeachers", "EducationPortal")',
                dataType: "json"
            },
            update: {
                url: '@Url.Action("UpdateTeachers", "EducationPortal")',
                dataType: "json"
            },
            parameterMap: function (options, operation) {
                if (operation !== "read" && options.models) {
                    return { models: kendo.stringify(options.models) };
                }
            }
        },
        batch: true,
        schema: {
            model: {
                id: "TeacherId",
                fields: {
                    TeacherId: { type: "number" },
                    FullName: { type: "string" },
                    IsHeadmaster: { type: "boolean" }
                }
            }
        }
    },
    toolbar: ["create", "save"],
    columns: [
        { field: "FullName", title: "Teacher" },
        { field: "IsHeadmaster", title: "Is a Headmaster?", width: "120px" },
        { command: ["destroy"], title: "&nbsp;", width: "85px" }],
    editable: true
});

我根据 Kendo 的示例改编了这段代码.问题是,从未调用 UpdateTeachers 方法.我怀疑问题出在 parameterMap 函数上,因为那是我最不了解的代码部分.

I adapted this code from Kendo's examples. The problem is, the UpdateTeachers method is never called. I suspect that the issue lies in the parameterMap function, because that's the part of the code I understand the least.

推荐答案

而不是使用

public void UpdateTeachers(IEnumerable<Teacher> updatedTeachers)
{
    // this is never called
}

使用过

public JsonResult UpdateTeachers(string models)
{
//Deserialize to object
IList<Teacher> teachers= new JavaScriptSerializer().Deserialize<IList<Teacher>>(models);

return Json(Teacher)
}

请注意,parameterMap: function() 以序列化字符串格式发送更新的数据,名称为 models,因此您应该在操作中使用models"作为参数名称

Note that parameterMap: function() send updated data in serialize string format with name models so you should use "models" as parameter name in your action

希望对你有帮助

这篇关于为什么我的 KendoGrid 不调用我的 MVC 控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆