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

Error handling of Ajax call

4 Answers 1461 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Vlad
Top achievements
Rank 1
Vlad asked on 09 Oct 2012, 08:18 PM
Hi All,

I am making ajax call from grid to the server side and when there is an error happen on the server than no error shown on the grid.  I am using example from trial download on Hierarchical example

Here is view code:
@(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.EmployeeViewModel>()
        .Name("Employees")
        .Columns(columns =>
        {
            columns.Bound(e => e.FirstName).Width(140);
            columns.Bound(e => e.LastName).Width(140);
            columns.Bound(e => e.Title).Width(200);
            columns.Bound(e => e.Country).Width(200);
            columns.Bound(e => e.City);
        })
        .ClientDetailTemplateId("employeesTemplate")
        .Pageable()
        .DataSource(dataSource => dataSource
            .Ajax()
            .Read(read => read.Action("HierarchyBinding_Employees", "Grid"))
            .PageSize(5)
        )
        .Sortable()
        .Events(events => events.DataBound("dataBound"))
)
 
<script id="employeesTemplate" type="text/kendo-tmpl">
    @(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.OrderViewModel>()
            .Name("Orders_#=EmployeeID#")
            .Columns(columns =>
            {
                columns.Bound(o => o.OrderID).Width(101);
                columns.Bound(o => o.ShipCountry).Width(140);
                columns.Bound(o => o.ShipAddress).Width(200);
                columns.Bound(o => o.ShipName).Width(200);
            })
            .DataSource(dataSource => dataSource
                .Ajax()
                .Read(read => read.Action("HierarchyBinding_Orders", "Grid", new { employeeID = "#=EmployeeID#" }))
            )
            .Pageable()
            .Sortable()
            .ToClientTemplate()
    )
</script>
<script>
    function dataBound() {
        this.expandRow(this.tbody.find("tr.k-master-row").first());
    }
</script>

and here is controller code

using System;
using System.Web.Mvc;
using System.Linq;
using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
 
namespace Kendo.Mvc.Examples.Controllers
{
    public partial class GridController : Controller
    {
        public ActionResult Hierarchy()
        {
            return View();
        }
 
        public ActionResult HierarchyBinding_Employees([DataSourceRequest] DataSourceRequest request)
        {
            throw new Exception("Test");        
        }
 
        public ActionResult HierarchyBinding_Orders(int employeeID, [DataSourceRequest] DataSourceRequest request)
        {
            return Json(GetOrders()
                .Where(order => order.EmployeeID == employeeID)
                .ToDataSourceResult(request));
        }
    }
}

My question is if grid itself doesn't handles exception on Ajax call, how to handle those sort of exceptions?

Thanks.

4 Answers, 1 is accepted

Sort by
0
Accepted
Alexander Valchev
Telerik team
answered on 12 Oct 2012, 06:19 AM
Hi Vlad,

The error event of the DataSource fires when an error occurs during data read or sync. The event arguments are the same as the ones of the error event of $.ajax(). I suggest you to hook up to that event and handle the error according to your requirements.
.DataSource(dataSource => dataSource
    .Ajax()
    .Read(read => read.Action("HierarchyBinding_Orders", "Grid", new { employeeID = "#=EmployeeID#" }))
    .Events(e => e.Error("onError"))
)


Kind regards,
Alexander Valchev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Vlad
Top achievements
Rank 1
answered on 23 Oct 2012, 02:47 PM
Hi Alexander,

Thanks for your help.  Works great.
0
mark baer
Top achievements
Rank 1
answered on 15 Jul 2014, 11:20 PM
How do you handle errors when you use the Kendo Client side/javascript controls instead of Razor?  Looking for samples, but not finding much.  Thanks
0
Alexander Valchev
Telerik team
answered on 18 Jul 2014, 01:14 PM
Hi Mark,

Error event is available in JavaScript syntax too. In the corresponding documentation you can find code sample:

Regards,
Alexander Valchev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Tags
Grid
Asked by
Vlad
Top achievements
Rank 1
Answers by
Alexander Valchev
Telerik team
Vlad
Top achievements
Rank 1
mark baer
Top achievements
Rank 1
Share this question
or