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("Id").HeaderText("ID").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).IsPrimaryKey(true).Visible(false).Add();
        col.Field("codigo").HeaderText("Código").Width("20").Add();
        col.Field("nombre").HeaderText("Nombre").Width("80").Add();
        col.Field("basico").HeaderText("Sueldo Básico").Width("40").EditType("numericedit").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
        col.Field("sede").HeaderText("Sede").Width("20").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 1, 2019 11:21 PM

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. 

Regards, 
Madhu Sudhanan P 



LU luis April 2, 2019 03:58 AM


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


PS Pavithra Subramaniyam Syncfusion Team April 2, 2019 11:28 PM

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() 
 
<script> 
    function failure(args) { 
        var errorMessage = args.error[0].error.responseText.split("Exception:")[1].split('<br>')[0];  //extract the message from args 
        alert(errorMessage); 
    } 
</script> 
 
[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); 
            } 
            else 
            { 
                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: 
 
 
Regards, 
Pavithra S. 
 



LU luis April 3, 2019 07:27 AM

Worked, Perfect...

Thanks.


HJ Hariharan J V Syncfusion Team April 3, 2019 09:07 PM

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.  

Regards,
Hariharan

Loader.
Live Chat Icon For mobile
Up arrow icon