# Ajax Binding

You can configure the Grid component for Ajax binding.

When configured for Ajax binding, the Grid for ASP.NET Core sends Ajax requests for paging, sorting, filtering, grouping, and saving data. For a runnable example, refer to the [Grid Ajax binding demo](https://demos.telerik.com/aspnet-core/grid/remote-data-binding).

The Ajax-bound mode has the following features:

* The Grid retrieves only the data (in JSON format) representing the current page. As a result, only the Grid is updated.
* All Grid templates (column, detail) are executed client-side. They follow the [Kendo UI for jQuery template](https://docs.telerik.com/kendo-ui/framework/templates/overview) definition rules and may contain embedded JavaScript code.

> tip The default casing for JSON strings in ASP.NET Core is camelCase. The Telerik UI components that are data-bound depend on PascalCase formatted response from the server. If the JSON serialization isn't configured properly, the UI components will display wrong data. To find out how to configure the application to return the data in Pascal-case, refer to the [JSON Serialization](https://www.telerik.com/aspnet-core-ui/documentation/installation/json-serialization) article.

## Configuring Ajax Binding to Database Data

To configure the Grid for ASP.NET Core for Ajax binding to the **Products** table in a sample database, follow these steps:

1. Create a new ASP.NET Core web application. Follow the [Getting Started with Telerik UI for ASP.NET Core](https://www.telerik.com/aspnet-core-ui/documentation/getting-started-core/first-steps) article to add Telerik UI for ASP.NET Core to the application.

2. Install the `Microsoft.EntityFrameworkCore` NuGet package.

3. Install `Microsoft.EntityFrameworkCore.SqlServer` package.

4. Install `Microsoft.EntityFrameworkCore.Tools` package.

5. Run a `Scaffold-DbContext` command in the Visual Studio Package Management Console. It generates models from the tables in a database called `Sample`. For more information, refer to [Scaffold a model from an existing database in EF Core](https://docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/existing-db#reverse-engineer-your-model).

   ```powershell
   Scaffold-DbContext 'Server=(localdb)\MSSQLLocalDB;Integrated Security=true;AttachDbFileName=D:\MyProjects\TelerikAspNetCoreApp1\TelerikAspNetCoreApp1\App_Data\Sample.mdf;Trusted_Connection=True;' Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Context SampleEntities
   ```

Note that the command specifies the path to the database `Sample.mdf` file.

1. Open the `HomeController.cs` and add a new action method which will return the Products as JSON. The Grid makes Ajax requests to this action.

   ```C#
   public ActionResult Products_Read()
   {
   }
   ```

2. Add a new parameter of type `Kendo.Mvc.UI.DataSourceRequest` to the action. It will contain the current Grid request information—page, sort, group, and filter. Decorate that parameter with the `Kendo.Mvc.UI.DataSourceRequestAttribute`. This attribute will populate the `DataSourceRequest` object from the posted data. Now import the `Kendo.Mvc.UI` namespace.

   ```C#
   public ActionResult Products_Read([DataSourceRequest]DataSourceRequest request)
   {
   }
   ```

3. Use the `ToDataSourceResult` extension method to convert the Products to a `Kendo.Mvc.UI.DataSourceResult` object. This extension method will page, filter, sort, or group your data using the information provided by the `DataSourceRequest` object. To use the `ToDataSourceResult` extension method, import the `Kendo.Mvc.Extensions` namespace.

   ```C#
   public ActionResult Products_Read([DataSourceRequest]DataSourceRequest request)
   {
       using (var northwind = new SampleEntities())
       {
           IQueryable<Product> products = northwind.Products;
           DataSourceResult result = products.ToDataSourceResult(request);
           return Json(result);
       }
   }
   ```

4. Return `DataSourceResult` as JSON. Alternatively, you can use an asynchronous action and return `ToDataSourceResultAsync` as JSON.

   ```C#
   public ActionResult Products_Read([DataSourceRequest]DataSourceRequest request)
   {
       using (var northwind = new SampleEntities())
       {
           IQueryable<Product> products = northwind.Products;
           DataSourceResult result = products.ToDataSourceResult(request);
           return Json(result);
       }
   }
   ```

   The asynchronous `ToDataSourceResultAsync` counterpart:

   ```C#
   public async Task<ActionResult> Products_Read([DataSourceRequest]DataSourceRequest request)
   {
       using (var northwind = new SampleEntities())
       {
           IQueryable<Product> products = northwind.Products;
           DataSourceResult result = await products.ToDataSourceResultAsync(request);
           return Json(result);
       }
   }
   ```

5. In the view, configure the Grid to use the action method created in the previous steps.

   ```Razor HtmlHelper
       @(Html.Kendo().Grid<TelerikAspNetCoreApp1.Models.Products>()
           .Name("grid")
           .DataSource(dataSource => dataSource //Configure the Grid data source.
               .Ajax() //Specify that Ajax binding is used.
               .Read(read => read.Action("Products_Read", "Home")) // Set the action method which will return the data in JSON format.
           )
           .Columns(columns =>
           {
               //Create a column bound to the ProductId property.
               columns.Bound(product => product.ProductId);
               //Create a column bound to the ProductName property.
               columns.Bound(product => product.ProductName);
               //Create a column bound to the UnitsInStock property.
               columns.Bound(product => product.UnitsInStock);
           })
           .Pageable() // Enable paging
               .Sortable() // Enable sorting
       )
   ```

   ```Razor TagHelper
   <div style="width:500px">
       <kendo-grid name="grid">
           <datasource type="DataSourceTagHelperType.Ajax">
               <transport>
                   <read url="@Url.Action("Products_Read", "Home")" />
               </transport>
           </datasource>
           <pageable enabled="true">
           </pageable>
           <sortable enabled="true"/>
           <columns>
               <column field="ProductID"/>
               <column field="ProductName"/>
               <column field="UnitsInStock"/>
           </columns>
       </kendo-grid>
   </div>
   ```

6. Build and run the application.

   ![UI for ASP.NET Core The final result is a Grid bound to data](https://www.telerik.com/aspnet-core-ui/documentation/assets/753cc727455a276583c31f63b83aeb7e/grid-binding-core.png)

To download a Visual Studio project, refer to the [Grid Ajax binding sample repository on GitHub](https://github.com/telerik/ui-for-aspnet-core-examples/blob/master/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Views/Grid/AjaxBinding.cshtml).

## See Also

* [ASP.NET Core DataGrid Homepage](https://www.telerik.com/aspnet-core-ui/grid)

* [DataSource Overview](https://www.telerik.com/aspnet-core-ui/documentation/html-helpers/datasource/overview)

* [Grid FAQs](https://www.telerik.com/aspnet-core-ui/documentation/html-helpers/data-management/grid/faq)

* [Custom Ajax Binding by the Grid HtmlHelper for ASP.NET Core (Demo)](https://demos.telerik.com/aspnet-core/grid/customajaxbinding)

* [Knowledge Base Articles](https://www.telerik.com/aspnet-core-ui/documentation/knowledge-base)

* [Server-Side API](https://www.telerik.com/aspnet-core-ui/documentation/api/grid)
