I can't get failure of delete action to work correctly in the crud grid examples
If I return an error from the server (usually because foreign key relation prevents delete):
public ActionResult DeleteModel([DataSourceRequest] DataSourceRequest request, tb_model_list model)
{
...
...
ModelState.AddModelError("modelcode", "Can't Delete.");
return Json(ModelState.ToDataSourceResult());
}
Marcel
4 Answers, 1 is accepted
(My apologies if that's part of the code you snipped out with the ellipses).
You might also want to post your view file.
a failure of update/insert works ok: the edit mode stays active and an error is displayed.
when a failure of delete (due to foreign key relation) happens, then I see the record disappear from the grid and then I see this javascript error :
0x800a138f - Microsoft JScript runtime error: Unable to get value of the property 'element': object is null or undefined
Thank You
Marcel
Basically when error is returned from the server after delete operation you should handle it manually using the DataSource Error event. For example you can clear the "destroyed" collection and use the DataSource Read method to refresh the Grid. Please check the example below:
Define Error event handler:
.DataSource(dataSource => dataSource
.Ajax()
.Events(e => e.Error(
"onError"
))
onError function:
function
onError(e) {
e.sender.read();
e.sender._destroyed = [];
alert(
"Delete was unsuccessful!"
);
}
Edit: You can also use the CancelChanges method of the Grid to cancel all pending changes (including unsuccessfully deleted records).
Kind Regards,
Vladimir Iliev
the Telerik team
that worked,
note that I already used the error function to show insert/update errors
so I handled your instructions in this way :
I'm not sure if it's the best way, but it seems to work :
controller :
public ActionResult DeleteModel([DataSourceRequest] DataSourceRequest request, tb_model_list model)
{
...
...
ModelState.AddModelError("modelcode", "delete");
return Json(ModelState.ToDataSourceResult());
}
view :
function error(args) {
if (args.errors) {
var grid = $("#tb_model_list").data("kendoGrid");
grid.one("dataBinding", function (e) {
e.preventDefault(); // cancel grid rebind if error occurs
for (var error in args.errors) {
if (args.errors[error].errors != "delete") {
showMessage(grid.editable.element, error, args.errors[error].errors);
}
else {
args.sender.read();
args.sender._destroyed = [];
alert("Delete was unsuccessful!");
}
}
});
}
}
Thanks,
Marcel