This is a migrated thread and some comments may be shown as answers.

Row removed from grid before server call

1 Answer 62 Views
Grid
This is a migrated thread and some comments may be shown as answers.
KRichter
Top achievements
Rank 1
KRichter asked on 05 Dec 2013, 12:07 PM
Hi,

I have a simple grid with only the Read and Destroy actions defined for the datasource. I also have an error event handler defined. While playing around with this setup, I noticed, that the row is removed as soon as I click the destroy button, even before the controller is called. If an error occures in the controller, my error handler does, what it should, but the grid is not refreshed. Do I have to do that by myself (If so, how do i do this in the error handler?) or did I make a mistake?

My View:
@(Html.Kendo().Grid<StorageClasses>()
            .Name("StorageClassesGrid")
            .ToolBar(toolbar => toolbar.Template("<button id='btnAddSC' class='k-button'><span class='k-icon k-add'></span></button>"))
            .Columns(columns =>
            {
                columns.Command(command =>
                {
                    command.Destroy().Text(" ").HtmlAttributes(new { style = "width:14px;min-width:27px" });
                }).Width("40px;");
                columns.Bound(p => p.Name);
                columns.Bound(p => p.Remark);
            })
                .Pageable(pager => pager.Refresh(true))
                .Sortable()
                .Scrollable(s => s.Height("auto"))
                .Filterable()
                .Resizable(r => r.Columns(true))
                .DataSource(dataSource => dataSource
                    .Ajax()
                    .ServerOperation(false)
                    .Model(model => model.Id(m => m.ID))
                    .Read(read => read.Action("GetStorageClassesByLocation", "Locations", new { LocationID = Model.ID }))
                    .Destroy(destroy => destroy.Action("StorageClass_Delete", "Locations", new { LocationID = Model.ID }))
                    .Events(e => e.Error("onGridError"))
                )
                .Events(e => e.Remove("onGridRowRemove").DataBound("resizeTabs"))
            )

Controller:
[HttpPost]
        public ActionResult StorageClass_Delete([DataSourceRequest] DataSourceRequest request, long? LocationID, StorageClasses StorageClass)
        {
            Exception exception;
 
            if (StorageClass != null && LocationID != null)
            {
                try
                {
                    if (ModelState.IsValid)
                    {
                        // delete entity in database
                        var result = _db.StorageClass_Delete(
                                    LocationID.HasValue ? LocationID.Value : 0,
                                    StorageClass.ID,
                                    ClientName,
                                    "WEB",
                                    UserName,
                                    out exception);
 
                        if (exception != null)
                        {
                            AddException(exception);
                        }
 
                        // check database action result
                        if (result != null && result.ErrorID == 0)
                        {
                            // database action successful
                            return Json(new[] { StorageClass }.ToDataSourceResult(request, ModelState));
                        }
                        else
                        {
                            ModelState.AddModelError("StorageClass_Delete", result.ErrorMessage);
                            return Json(new[] { StorageClass }.ToDataSourceResult(request, ModelState));
                        }
                    }
                }
                catch (Exception ex)
                {
                    AddException(ex);
                    ModelState.AddModelError("StorageClass_Delete", ex.Message);
                    return Json(new[] { StorageClass }.ToDataSourceResult(request, ModelState));                   
                }
            }
            ModelState.AddModelError("StorageClass_Delete", "Parameter Fehler.");
            return Json(new[] { StorageClass }.ToDataSourceResult(request, ModelState));
        }

Error Handler:
function onGridError(e) {
        var txt = "";
 
        $.each(e.errors, function (propertyName) {
            txt = txt +this.errors + " ";
        });
 
        $("#lblStatus").text(txt);
        $("#lblStatus").css('color', 'red', 'font-weight ', 'bold');
    }

Best regards

Dietmar

1 Answer, 1 is accepted

Sort by
0
Alexander Popov
Telerik team
answered on 05 Dec 2013, 02:55 PM
Hello Dietmar,

Yes, refreshing the Grid should be done manually in the error handler in case the delete operation failed. For example you could call the Grid's cancelChanges method or read the data again using the DataSource read method.

Regards,
Alexander Popov
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
Grid
Asked by
KRichter
Top achievements
Rank 1
Answers by
Alexander Popov
Telerik team
Share this question
or