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. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to return Custom message from Controller when CRUD action fails

Thread ID:

Created:

Updated:

Platform:

Replies:

143686 Apr 1,2019 08:52 AM UTC Apr 4,2019 05:07 AM UTC ASP.NET MVC - EJ 2 5
loading
Tags: Grid
luis
Asked On April 1, 2019 08:52 AM UTC

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



Madhu Sudhanan P [Syncfusion]
Replied On 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. 

Regards, 
Madhu Sudhanan P 


luis
Replied On April 2, 2019 11:58 AM UTC


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

Pavithra Subramaniyam [Syncfusion]
Replied On 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() 
 
<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. 
 


luis
Replied On April 3, 2019 03:27 PM UTC

Worked, Perfect...

Thanks.

Hariharan J V [Syncfusion]
Replied On 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.  

Regards,
Hariharan

CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon