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
- ToDataSourceResultAsync - QueryableExtensions
- Inline Editing - Kendo UI Grid Live Demo
- Asynchronous Programming - Microsoft Documentation
- Client-Side API Reference of the Grid for ASP.NET MVC
- Server-Side API Reference of the Grid for ASP.NET MVC
- Telerik UI for ASP.NET MVC Breaking Changes
- Telerik UI for ASP.NET MVC Knowledge Base