简体   繁体   English

Kendo Ajax在网格中删除

[英]Kendo Ajax Delete in a grid

Using Kendo Grid and MVC 4 使用Kendo Grid和MVC 4

In my controller when it errors out I send a error message: 在控制器出现错误时,我会发送一条错误消息:

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

The error is nicely displayed, however the item is visually removed from the grid, even though not from the data source. 该错误可以很好地显示,但是即使没有从数据源中,也可以从网格中直观地删除该项目。 Refreshing the grid puts it back. 刷新网格将其放回原处。

How do I have the grid not delete a record visually when it receives an error in the delete method? 当网格在delete方法中收到错误时,如何不以可视方式删除记录?

To pass error message from controller to view , you need to use ModelState, see below: 要将错误消息从控制器传递到view,您需要使用ModelState,如下所示:

ModelState.AddModelError("Delete", "my custom error");
return Json(ModelState.ToDataSourceResult());

You also can use an empty string for the key: 您还可以为密钥使用空字符串:

ModelState.AddModelError(string.Empty, "my custom error");
return Json(ModelState.ToDataSourceResult());

Update 更新

Without your code, it's hard to imagine where the problem is exactly. 没有您的代码,很难想象问题出在哪里。 You can try below code and see how it works for you . 您可以尝试下面的代码,看看它如何为您工作。 But maybe the problem comes from your Kendo grid code. 但是也许问题出在您的Kendo网格代码上。

   var model = new YourBuildingModel();
   model = GetModelList();
   //
   ModelState.AddModelError(string.Empty, "my custom error");
   var buildingData = (new List<YourBuildingModel> {model}).ToDataSourceResult(request, ModelState);
   return Json(buildingData, JsonRequestBehavior.AllowGet);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM