We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

How to return Custom message from Controller when CRUD action fails

The code below fires correctly on the grid  ActionFailure but the alert message show "Undefined".


 @Html.EJS().Grid("Grid").DataSource(ds => { ds.Url("/Cargos/UrlDatasource").InsertUrl("/Cargos/Insert").UpdateUrl("/Cargos/Update").RemoveUrl("/Cargos/Delete").Adaptor("UrlAdaptor"); }).ToolbarClick("toolbarClick").ActionComplete("actionComplete").RowSelected("rowSelected").Columns(col =>
        col.Field("basico").HeaderText("Sueldo Básico").Width("40").EditType("numericedit").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
    }).DataBound("dataBound").ActionFailure("actionFailure").AllowPaging(true).EditSettings(edit => { edit.AllowAdding(true).AllowEditing(true).AllowDeleting(true).ShowDeleteConfirmDialog(true).Mode(Syncfusion.EJ2.Grids.EditMode.Dialog).Template("#dialogtemplate"); }).Toolbar(toolbarItems).Locale("es").Render()


public ActionResult Insert(CargoSet value)
            var obj = db.CargoSet.Where(o => o.codigo == value.codigo).SingleOrDefault();
            if (obj != null) //Check already exisiting
                throw new InvalidOperationException("Cargo ya existe para esa Sede");//Exception thrown if exisiting              

function actionFailure(args) {

 alert(args.error.statusText); //get the exception message

Please help. I'm using EJ2

5 Replies

MS Madhu Sudhanan P Syncfusion Team April 2, 2019 07:21 AM UTC

Hi Luis, 

Thanks for contacting Syncfusion support. 

During AJAX requests simply throwing exception at the server side will not return the formatted error details. Please refer to the below help links to write statuscode and error details at the server side.  

When the error is returned as specified in the above links, you can access them at the actionFailure event properly. 

Madhu Sudhanan P 

LU luis April 2, 2019 11:58 AM UTC

Okay, could you give me some guidelines?,  that examples es very old.

PS Pavithra Subramaniyam Syncfusion Team April 3, 2019 07:28 AM UTC

Hi Luis, 
Thanks for your update. 
We have analyzed your query and found that you have not properly extracted the custom message in ActionFailure event. Please refer the following code snippet to achieve your requirement. 
[Client side code] 
@Html.EJS().Grid("Grid").DataSource(ds => ds.Url("/Home/UrlDatasource").Adaptor("UrlAdaptor").InsertUrl("/Home/Insert") 
.RemoveUrl("/Home/Remove").UpdateUrl("Home/Update")).AllowPaging(true).Columns(col => 
    col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Width("120").Add(); 
}).Toolbar(new List<string>() { "Add", "Edit", "Delete", "Update", "Cancel" }).ActionFailure("failure").EditSettings(edit => { edit.AllowAdding(true).AllowDeleting(true).AllowEditing(true); }).Render() 
    function failure(args) { 
        var errorMessage = args.error[0].error.responseText.split("Exception:")[1].split('<br>')[0];  //extract the message from args 
[Server side code] 
public ActionResult Insert(Orders value) 
            var data = order.Where(or => or.OrderID == value.OrderID).FirstOrDefault(); 
            if (data == null) 
                order.Insert(0, value); 
                throw new Exception("Duplicate values cannot be inserted"); //Add custom exception message 
            return Json(value); 
We have attached a sample for your reference. Please find the sample below: 
Pavithra S. 

LU luis April 3, 2019 03:27 PM UTC

Worked, Perfect...


HJ Hariharan J V Syncfusion Team April 4, 2019 05:07 AM UTC

Hi Luis,

Thanks for your update. 
We are happy to hear that the provided solution helped you.  
Please contact us if you need any further assistance. As always, we will be happy to assist you.  


Live Chat Icon For mobile
Up arrow icon