teleriktelerik-gridtelerik-mvc

How cancel removed item from kendo grid when got exception on method at controller [Telerik]?


Telerik UI for ASP.NET MVC

I want set new values for @ViewBag when edit, delete, add new value in kendo grid for handle not success changed at database (catched one exception) and refresh kendo grid to show older values without edited, added or removed item.

My problem is like this:

Controller

[HttpPost]
public ActionResult RemoveX(int idX){
     Model.typeX removeX = //get object from context with idX
     try
     {
          //removeX get out of db
     }
     catch(Exception e)
     {
          ViewBag.Flag = "true"; //"flag to show kendo window in view"
          return //tried many returns to new ViewBag value go to my view, nothing working.
     }
     return Json(ModelState.ToDataSourceResult());
}

Controller constructor

ViewBag.Flag = "false";

View

@(Html.Kendo().Grid<Project.Models.ModelX>()
  @* many columns and options*@
  .DataSource(dataSource => dataSource
     .Ajax()
     .PageSize(10)
     .Events(events => events
          .Error("errorFunction")
          .Change("gridAfterChange"))
     .Model(model => {@*some ViewBag from controller*@})
     .Create(update => update.Action("MethodController", "Controller"))
     .Read(read => read.Action("MethodController", "Controller"))    
     .Update(update => update.Action("MethodController", "Controller"))
     .Destroy(update => update.Action("RemoveX", "Controller"))
  )
)

View javascript

function gridAfterChange(e) {
     if('(@ViewBag.Flag)' == "true")
          @ViewBag.Flag= "false";
          var grid = $("#gridName").data("kendoGrid");
          grid.dataSource.read();
          var otherWindow = $("#otherWindow").data("kendoWindow");
          otherWindow.open.center();
     }
}

I want show one window when not have success on edit, delete or add new item. When throw new Exception() inside beginning of try{} for test kendo grid still remove item from grid. I'm trying refresh() kendo grid when catch any Exception.

I searched in many locations, but nothing work's here. Anyone can help or have another method to do this?

I saw this link: cancel changes of kendo ui for jquery doc's

but didn't found anything like that for telerik ui for asp.net mvc.


Solution

  • when controller catch Exception trigger the event .Error of grid and I can show popup window and refresh grid there.

    =>Solution:

    Controller

    [HttpPost]
    public ActionResult RemoveX(int idX){
        Model.typeX removeX = //get object from context with idX
        try
        {
            throw new Exception(); //for test
            //removeX get out of db
        }
        catch(Exception ex)
        {
            ModelState.AddModelError("", ex.Message);
            return Json(ModelState.ToDataSourceResult());
        }
        return Json(ModelState.ToDataSourceResult());
    }
    

    View

    @(Html.Kendo().Grid<Project.Models.ModelX>()
        @* many columns and options*@
        .DataSource(dataSource => dataSource
            .Ajax()
            .PageSize(10)
            .Events(events => events
                .Error("errorFunction")
            )
        .Model(model => {@*some ViewBag from controller*@})
        .Create(update => update.Action("AddMethodAtController", "Controller"))
        .Read(read => read.Action("FillDataSourceMethodAtController", "Controller"))
        .Update(update => update.Action("EditMethodAtController", "Controller"))
        .Destroy(update => update.Action("RemoveX", "Controller"))
        )
    )
    @(Html.Kendo().Window()
        .Name("popupWindow")
        .Title("Error")
        .Visible(false)
    )
    <script type="text/javascript">
        function errorFunction(e) {
            if(e.erros){
                var grid = $("#gridName").data("kendoGrid");
                grid.dataSource.read();
                grid.refresh();
                var popupWindow= $("#popupWindow").data("kendoWindow");                
                var message = e.errors[""]["errors"][0];
                popupWindow.content(message);
                popupWindow.open()
                popupWindow.center();                
            }
        }
    </script>
    

    Original file and logics are very different, I put solution as example. I have if statmente for handle only the catch error on try/catch and open this window only for these error of exception. And at controller I have one other controller for make message pattern for all exceptions we take care and one default message for not handle exceptions, and logic for trace all handle and not handle exceptions. To care of all exceptions. And these errors are linked with other project of HelpDesk.

    Many thanks for all try help.