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

problem with mvc crud grid examples : destroy function

4 Answers 284 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Marcel
Top achievements
Rank 1
Marcel asked on 07 Jan 2013, 01:56 PM
Hello,

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());
        }

then the row is still deleted from the grid ?

Marcel

4 Answers, 1 is accepted

Sort by
0
Eric
Top achievements
Rank 1
answered on 07 Jan 2013, 09:00 PM
Could you post the rest of your DeleteModel method? From the block you have, it looks like you might not be detecting the error correctly and have actually already deleted the entry on the server side.

(My apologies if that's part of the code you snipped out with the ellipses).

You might also want to post your view file.
0
Marcel
Top achievements
Rank 1
answered on 08 Jan 2013, 08:21 AM
files are  attached

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
0
Vladimir Iliev
Telerik team
answered on 09 Jan 2013, 01:01 PM
Hi 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
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Marcel
Top achievements
Rank 1
answered on 09 Jan 2013, 03:14 PM
Ok, thank you

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

 

Tags
Grid
Asked by
Marcel
Top achievements
Rank 1
Answers by
Eric
Top achievements
Rank 1
Marcel
Top achievements
Rank 1
Vladimir Iliev
Telerik team
Share this question
or