Why doesn't my KendoGrid call my MVC controller?

这一生的挚爱 提交于 2019-12-01 11:57:26

问题


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
}

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
});

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.


回答1:


instead of using

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

used

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

return Json(Teacher)
}

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

i hope this will help you




回答2:


I had the same issue. I kept rechecking my grid thinking I had made a mistake there. It turned out that my problem was in the object I was passing into the grid: my object's Id was not set when it was passed in.

I checked in Fiddler, and the POST was still being made, but without the object's ID my controller didn't recognize the parameters, thus not hitting my action.

Make sure the objects returned by your read action have their properties set.



来源:https://stackoverflow.com/questions/22546978/why-doesnt-my-kendogrid-call-my-mvc-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!