Kendo UI网格更新事件未触发

时间:2014-06-03 14:03:30

标签: asp.net-mvc kendo-ui kendo-grid

我有Kendo UI内联网格。它可以正确读取和填充网格。但是当我按任何列中的编辑和更改并按更新时,更新事件不会触发。 它也没有调用控制器方法。

希望有人可以帮助我指出我在这里做错了什么。 以下是我的网格绑定。     

    dataSource = new kendo.data.DataSource({
        transport: {

            read: function (options) {
                $.ajax({
                    type: 'POST',
                    url: "./GetIngredients",
                    dataType: "json",
                    success: function (result) {
                        options.success(result);
                    }
                });
            },

            update: function (options) {
                $.ajax({
                    type: 'POST',
                    url: "./UpdateData",
                    dataType: "json",
                    contentType: "application/json; charset=utf-8"
                });
            },
            parameterMap: function (options, operation) {
                alert('1');
                if (operation !== "read" && options.models) {
                    alert('2');
                    return { models: kendo.stringify(options.models) };
                }
            }
        },
        batch: true,
        pageSize: 20,
        schema: {
            model: {
                id: "Id",
                fields: {
                    Id: { editable: false, nullable: true },
                    Division: { type: "string", editable: true, nullable: false },
                    GroupName: { type: "string", validation: { required: true } },
                    CategoryName: { type: "string" },
                    TypeName: { type: "string" },
                    ItemName: { type: "string" }
                }
            }
        }

    });

    var grid = $("#grid").kendoGrid(
   {
       dataSource: dataSource,
       height: 430,
       scrollable: true,
       pageable: true,
       navigatable: true,
       columns: [
       { field: "Division", title: "Division", width: "80px" },
       { field: "GroupName", title: "Group Name", width: "70px" },
       { field: "CategoryName", title: "Category Name", width: "110px" },
       { field: "TypeName", title: "Type Name", width: "100px" },
       { field: "ItemName", width: "140px" },
       { command: ["edit", "destroy"], title: " ", width: "170px" }],
       editable: "inline"
   }).data("kendoGrid");

以下是Homecontroller中的方法。

  [HttpPost]
    public JsonResult GetIngredients()
    {
        IngredientData ingredientData = new IngredientData();
        ingredientData.Id = 1;
        ingredientData.DivisionId = 1;
        ingredientData.Division = "Division Abc";
        ingredientData.GroupId = 2;
        ingredientData.GroupName = "Group -A";
        ingredientData.CategoryId = 3;
        ingredientData.CategoryName = "Category -D";
        ingredientData.FoodTypeId = 4;
        ingredientData.TypeName = "Type One";
        ingredientData.ItemId = 5;
        ingredientData.ItemName = "Item One";

        return Json( ingredientData , JsonRequestBehavior.AllowGet);
    }

 [HttpPost]
    public void UpdateData()
    {
        // logic for update data in database.
    }

2 个答案:

答案 0 :(得分:1)

您是否意识到batch设置为true?在batch模式下,您必须在网格定义中的syncsaveChanges上调用dataSource

尝试添加工具栏命令以调用saveChanges,如下所示:

var grid = $("#grid").kendoGrid({
    dataSource: dataSource,
    height: 430,
    scrollable: true,
    pageable: true,
    navigatable: true,
    toolbar: [ "save" ],
    columns: [
        { field: "Division", title: "Division", width: "80px" },
        { field: "GroupName", title: "Group Name", width: "70px" },
        { field: "CategoryName", title: "Category Name", width: "110px" },
        { field: "TypeName", title: "Type Name", width: "100px" },
        { field: "ItemName", width: "140px" },
        { command: ["edit", "destroy"], title: " ", width: "170px" }],
    editable: "inline"
}).data("kendoGrid");

或只是删除batch模式。

答案 1 :(得分:1)

我遇到过同样的问题。但我已经解决了这个问题。

要触发创建/删除/更新,我们需要在dataSource中指定模式(在模式中我们应该提到至少什么是id字段)。

架构:{                 型号:{                     id:" StudentID"                 }             }

代码:

var dataSource = new kendo.data.DataSource({
       transport: {
            read: function (options) {
                alert("read");              
            },
            create: function (options) {                    
                                   alert("create");             
            },
            update: function (options) {                    
                                   alert("update");                },
            destroy: function (options) {                    
                                   alert("destroy");                }
        },
                    schema: {
            model: {
                id: "StudentID"
            }
        }