Ian*_*ink 31 asp.net-mvc kendo-ui kendo-grid
在MVC4中使用Kendo UI 我有一个Grid,它使Ajax调用数据回到Controller中:
public ActionResult SearchUser_Read([DataSourceRequest]DataSourceRequest request)
{
var data = CreateAnExcaptionHere();
return Json(data.ToDataSourceResult(request));
}
Run Code Online (Sandbox Code Playgroud)
如何使用此调用通知页面存在错误?
Dre*_*ano 44
如果需要从服务器显示错误消息,则可以通过返回仅设置了Errors属性的DataSourceResult对象来执行此操作:
return this.Json(new DataSourceResult
{
Errors = "my custom error"
});
Run Code Online (Sandbox Code Playgroud)
并使用此功能在客户端上进行选择(由该.Events(events => events.Error("onError"))行引用):
function onError(e, status) {
if (e.status == "customerror") {
alert(e.errors);
}
else {
alert("Generic server error.");
}
}
Run Code Online (Sandbox Code Playgroud)
Ian*_*ink 25
找到它,Kendo通过向要调用的JS函数的DataSource添加一个Event来支持它.而已.
.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>
Run Code Online (Sandbox Code Playgroud)
Mat*_*att 11
只是稍微扩展Drew的答案:我们通常希望在发生错误时回滚Kendo Grid中的更改.否则,如果在从网格中删除项目时抛出错误,例如,即使抛出错误并显示消息,它仍将显示为已删除.
此函数还取消使用引发错误的数据源的任何网格中的更改:
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.");
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
34188 次 |
| 最近记录: |