asp.net-mvc Kendo:处理 Ajax 数据请求中的错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13653662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:26:40  来源:igfitidea点击:

Kendo: Handling Errors in Ajax Data Requests

asp.net-mvckendo-uikendo-grid

提问by Ian Vink

Using KendoUI in MVC4I have a Gridthat makes Ajaxcalls for data back into the Controller:

MVC4 中使用KendoUI我有一个网格,它使Ajax调用将数据返回到控制器:

    public ActionResult SearchUser_Read([DataSourceRequest]DataSourceRequest request)
    {
        var data = CreateAnExcaptionHere();
        return Json(data.ToDataSourceResult(request));
    }

How do I use this call to inform the page that there was an error?

如何使用此调用通知页面出现错误?

回答by Drew Delano

If you need to display an error message from the server then you can do it by returning a DataSourceResult object with only its Errors property set:

如果您需要显示来自服务器的错误消息,那么您可以通过返回一个只设置了 Errors 属性的 DataSourceResult 对象来实现:

return this.Json(new DataSourceResult
            {
                Errors = "my custom error"
            });

And pick it up on the client by using this (referenced by the .Events(events => events.Error("onError"))line):

并使用这个(由.Events(events => events.Error("onError"))行引用)在客户端上获取它:

function onError(e, status) {
    if (e.status == "customerror") {
        alert(e.errors);
    }
    else {
        alert("Generic server error.");
    }
}

回答by Ian Vink

Found it, Kendo supports it by just adding a Event to the DataSource the JS function to call. That's it.

找到了,Kendo 支持它,只需在 JS 函数的 DataSource 中添加一个事件即可调用。就是这样。

  .DataSource(dataSource => dataSource
      .Ajax()
      .Events(events => events.Error("onError"))
      .Read(read => read.Action("SearchUser_Read", "Search").Data("parentModel"))
  )

<script> 
    function onError(e, status) {
          alert("A server error has occurred!");
}
</script>

回答by Matt

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.

稍微扩展 Drew 的答案:我们通常希望在发生错误时也回滚 Kendo Grid 中的更改。否则,如果从网格中删除项目时抛出错误,例如,即使抛出错误并显示一条消息,它仍将显示为已删除。

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

}

回答by Sunny

Try to raise the exception and check whether it is prompting an alert message or not.

尝试引发异常并检查它是否正在提示警报消息。

For Kendo grid, there is error event which might be helpful for you.

对于剑道网格,有错误事件可能对您有所帮助。

http://docs.kendoui.com/documentation/getting-started/using-kendo-with/aspnet-mvc/migration/widgets/grid

http://docs.kendoui.c​​om/documentation/getting-started/using-kendo-with/aspnet-mvc/migration/widgets/grid

We used telerik mvc grids which automatically displays alert messages if there is any error while binding.

我们使用了 Telerik mvc 网格,如果绑定时出现任何错误,它会自动显示警报消息。

http://www.telerik.com/community/forums/aspnet-mvc/grid/exception-handling.aspx

http://www.telerik.com/community/forums/aspnet-mvc/grid/exception-handling.aspx

http://www.telerik.com/community/forums/aspnet-mvc/grid/how-to-do-error-handling-in-gridaction-methods.aspx

http://www.telerik.com/community/forums/aspnet-mvc/grid/how-to-do-error-handling-in-gridaction-methods.aspx

回答by user714157

How about

怎么样

throw new HttpResponseException(HttpStatusCode.BadRequest);