New to Telerik UI for ASP.NET Core? Download free 30-day trial

Add Asynchronous Calls to Action Methods With ToDataSourceResultAsync

Environment

Product Version 2020.1.114
Product Progress® Kendo UI® Grid for ASP.NET MVC

Description

What is the best approach to using Async/Await in the Kendo UI Grid's action methods?

Solution

Kendo.Mvc.Extensions contains QueryableExtensions which include the ToDatasourceResultAsync method for asynchronous programming. The following contains the action methods from the Kendo UI Grid's Editing Inline Live Demo using ToDataSourceResultAsync:

        public async Task<ActionResult> EditingInline_Read([DataSourceRequest] DataSourceRequest request)
        {
            var products = await productService.Read().ToDataSourceResultAsync(request);

            return Json(products);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public async Task<ActionResult> EditingInline_Create([DataSourceRequest] DataSourceRequest request, ProductViewModel product)
        {
            if (product != null && ModelState.IsValid)
            {
                productService.Create(product);
            }

            var result = await new[] { product }.ToDataSourceResultAsync(request, ModelState);

            return Json(result);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public async Task<ActionResult> EditingInline_Update([DataSourceRequest] DataSourceRequest request, ProductViewModel product)
        {
            if (product != null && ModelState.IsValid)
            {
                productService.Update(product);
            }

            var result = await new[] { product }.ToDataSourceResultAsync(request, ModelState);

            return Json(result);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public async Task<ActionResult> EditingInline_Destroy([DataSourceRequest] DataSourceRequest request, ProductViewModel product)
        {
            if (product != null)
            {
                productService.Destroy(product);
            }

            var result = await new[] { product }.ToDataSourceResultAsync(request, ModelState);

            return Json(result);
        }

See Also

In this article
Not finding the help you need? Improve this article