Kendo: Handling Errors in Ajax Data Requests

前端 未结 5 1121
耶瑟儿~
耶瑟儿~ 2020-12-17 08:23

Using Kendo UI in MVC4 I have a Grid that makes Ajax calls for data back into the Controller:

<         


        
5条回答
  •  [愿得一人]
    2020-12-17 08:29

    To extend Drew's answer just a little bit: we usually want to roll back the change in the Kendo Grid also when an error occurs. Otherwise, if an error is thrown as an item is deleted from the grid, for instance, it will still appear to be deleted even though the error was thrown and a message was shown.

    This function also cancels the changes in any grids that are using the data source that threw an error:

    function onError(e, status) {
    
        // Cancel changes on any grids on the page that are using this data source
        $('.k-grid').each(function (item) {
            var grid = $(this).data("kendoGrid");
            if (e.sender === grid.dataSource) {
                grid.cancelChanges();
            }
        });
    
        if (e.status == "customerror") {
            alert(e.errors);
        }
        else {
            alert("Generic server error.");
        }
    
    }
    

提交回复
热议问题