Adding Asynchronous Calls to Action Methods With ToDataSourceResultAsync
Environment
Product Version | 2020.1.114 |
Product | Progress® Telerik UI for ASP.NET Core Grid |
Description
What is the best approach to using Async/Await in the Telerik UI for ASP.NET Core 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 Core 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 Core
- Server-Side API Reference of the Grid for ASP.NET Core
- Telerik UI for ASP.NET Core Breaking Changes
- Telerik UI for ASP.NET Core Knowledge Base