kendo grid delete command not working

陌路散爱 提交于 2019-11-30 11:29:47

There are three common reasons delete won't work:


1. Not setting editable of grid to inline or popup. The deleted items will be automatically processed through transport destroy only for "inline"/"popup" edit modes. Ex:

editable: {
   mode: "inline",
}
//or
editable: "inline"


2. If on your datasource, you have the batch flag set to true, this means the datasource will make the call only after you tell it to, e.g calling sync(). Ex:

var dataSource = new kendo.data.DataSource({
    batch: true,
    //.....
});
//... in some where e.g in a save button click event call the following line:
dataSource.sync();


3. You should define id to your primary key of database field name inside model of datasource. Ex:

   model: {
        id: "ProductID",
        fields: {
            ProductID: { editable: false, nullable: true },
        }
    }


So the problem with your code is first one, i.e you did not set editable to inline or popup

If you choose not to include editable.mode in order to utilize the in-cell editing, you can set the toolbar of the grid to include the option save:

$("#grid").kendoGrid({
    dataSource: {
        transport: {
            ....
        },
        schema: {
            ....
        }
    },                        
    toolbar: ["create", "save", "cancel"],
    columns: [
        ....
    ],
    editable: true
});

This will create a save button at the toolbar of the grid. After deleting any records by clicking the destroy command button, click on the save button to have the grid to make an Ajax call to the server to delete the record.

If you would rather delete the record automatically without including the save button, you could add a change event handler to the datasource of the grid:

$("#grid").kendoGrid({
    dataSource: {
        transport: {
            ....
        },
        schema: {
            ....
        },
        change: function(e) {
            if (e.action === "remove") {
                this.sync();
            }
        }
    },                        
    columns: [
        ....
    ],
    editable: true
});

This will automatically sync the changes you made to the grid with the server when there's a data change.

Hmm try not including type: "POST", and see if it now works since as far as I can see that bit isn't included on the demo's and I don't think I included it when I last did inline edits/deletes.

I had put an arbitray name for an int on the server Delete Method.

    [HttpPost]
    public ActionResult DeleteRandomTest(Int32 randomTestId)
    {
         ...
    }

The default modelbinder was probably looking for a property called Id (same as the primary key of my type according to the configuration of the model).

 .Model(config => config.Id(p => p.Id))

In fact, I proved this by changing the signature to the following:

    [HttpPost]
    public ActionResult DeleteRandomTest(Int32 Id)
    {
        ...
    }

My break point was hit after that.

Ultimately, I used the full type as the parameter as shown in the Kendo examples because I didn't want to have poorly named parameter names (not camel case) in the action. Shown as follows:

    [HttpPost]
    public ActionResult DeleteRandomTest([DataSourceRequest]
         DataSourceRequest request, RandomDrugTest randomDrugTest)
    {
       ...
    }

This seems to the be the reason it wasn't working.

I had the same issue. My issue was caused by having a data property in the kendo model. Example:

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