Articles in this section
Category / Section

How to display custom error message on AJAX action failture Solution?

2 mins read

Solution:

We can display the error details in client side using ActionFailure client side event using ASP.NET MVC Grid

Example:

Grid rendering code:

JS:

<script type="text/javascript">
var dataManger = ej.DataManager({
                url: "http://mvc.syncfusion.com/Services/Northwnd.svc/Orders/"
            });
          $("#Grid").ejGrid({
                dataSource: dataManger,                
                allowPaging: true,
                columns: [
                        { field: "OrderID", headerText: "Order ID", width: 75, textAlign: ej.TextAlign.Right },
                        { field: "EmployeeID", headerText: "Employee ID", width: 75, textAlign: ej.TextAlign.Right },
                        { field: "Freight", width: 75, format: "{0:C}"},
                        { field: "OrderDate", headerText: "Order Date", width: 80, format: "{0:MM/dd/yyyy}", textAlign: ej.TextAlign.Right },
                        { field: "ShipCity", headerText: "Ship City", width: 110 },
                        { field: "ShipCountry", headerText: "Ship Country", width: 110 }
                ],
                actionFailure:"FailMessage"
});
</script>

 

MVC:

@(Html.EJ().Grid<SampleDemo.OrdersView>("FlatGrid")
.Datasource("http://mvc.syncfusion.com/Services/Northwnd.svc/Orders/")
.AllowPaging()
.Columns(col =>
        {
            col.Field("OrderID").HeaderText("Order ID").TextAlign(TextAlign.Right) .Width(75).Add();
            col.Field("EmployeeID").HeaderText("Employee ID").TextAlign (TextAlign.Right).Width(75).Add();
            col.Field("Freight").HeaderText("Freight").Width(75).Format("{0:C}").Add();
            col.Field("OrderDate").HeaderText("Order Date").TextAlign(TextAlign.Right).Width(80).Format("{0:MM/dd/yyyy}")").TextAlign (TextAlign.Right).Add();
            col.Field("ShipCity").HeaderText("Ship City").Width(110).Add();
            col.Field("ShipCountry").HeaderText("Ship Country").Width(110).Add();
        })
.ClientSideEvents(eve=>{eve.ActionFailure("FailMessage");})
    )
 

 

ASP.NET:

<ej:Grid ID="Grid1" runat="server" AllowPaging="True">
           <DataManager URL="http://mvc.syncfusion.com/Services/Northwnd.svc/Orders/">                        </DataManager>
           <Columns>  
                <ej:Column Field="OrderID" HeaderText="Order ID" TextAlign="Right" Width="75" />
                <ej:Column Field="EmployeeID" HeaderText="Employee ID" TextAlign="Right" Width="75" />
                <ej:Column Field="Freight" HeaderText="Freight" TextAlign="Right" Width="75" Format="{0:C}" />
                <ej:Column Field="OrderDate" HeaderText="Order Date" TextAlign="Right" Width="80" Format="{0:MM/dd/yyyy}" />
                <ej:Column Field="ShipCity" HeaderText="Ship City" Width="110" />   
                <ej:Column Field="ShipCountry" HeaderText="Ship Country" Width="110" />
</Columns>         
            <ClientSideEvents ActionFailure="FailMessage"/>
</ej:Grid>

 

JavaScript code:

<script type="text/javascript">
    function FailMessage(args) {
        // args.error.error().statusText will display the status of the action failure
       alert(args.error.error().statusText);    }
</script>

 

Result:

Status of action failure

                     Figure: Status of the action failure.



Conclusion

I hope you enjoyed learning how to display error details on the client side on MVC Grid.

You can refer to ASP.NET MVC Grid feature tour page to learn about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET MVC Grid example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please sign in to leave a comment
Access denied
Access denied