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

Adding Asynchronous Calls to Action Methods With ToDataSourceResultAsync

Environment

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

Description

What is the best approach to using Async/Await in the Telerik UI for ASP.NET MVC 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 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);
        }

The ViewModel definition can be found here:

Grid Core - Inline Editing Demo

More ASP.NET MVC Grid Resources

See Also

In this article