# Custom Binding

You can use a custom DataSource and bind the AutoComplete to a `ToDataSourceResult` instance.

## Setting Up the Project

1. Make sure you followed all the steps from the [introductory article on Telerik UI for ASP.NET Core](https://www.telerik.com/aspnet-core-ui/documentation/introduction).

2. Create an action method which renders the view.

   ```C#
       public ActionResult Index()
       {
           return View();
       }
   ```

3. Create a new action method and pass the **Products** table as JSON result.

   ```C#
       public JsonResult GetProducts([DataSourceRequest] DataSourceRequest request)
       {
           NorthwindDataContext northwind = new NorthwindDataContext();

           return Json(northwind.Products.ToDataSourceResult(request));
       }
   ```

4. Add an Ajax-bound AutoComplete.

   ```Razor HtmlHelper
       @(Html.Kendo().AutoComplete()
           .Name("productAutoComplete")
           .DataTextField("ProductName") // Specify which property of the Product to be used by the autocomplete as a text.
           .DataSource(source =>
           {
               source.Custom()
                       .ServerFiltering(true)
                       .Type("aspnetmvc-ajax") // Set this type if you want to use DataSourceRequest and ToDataSourceResult instances.
                       .Transport(transport =>
                       {
                           transport.Read("GetProducts", "Home");
                       })
                       .Schema(schema =>
                       {
                           schema.Data("Data") // Define the [data](https://docs.telerik.com/kendo-ui/api/data/datasource#configuration-schema.data) option.
                               .Total("Total"); // Define the [total](https://docs.telerik.com/kendo-ui/api/data/datasource#configuration-schema.total) option.
                       });
           })
       )
   ```

   ```Razor TagHelper
   <kendo-autocomplete name="productAutoComplete"
                       datatextfield="ProductName">
       <datasource type="DataSourceTagHelperType.Custom"
                   server-filtering="true"
                   custom-type="aspnetmvc-ajax">
           <transport>
               <read url="@Url.Action("GetProducts", "Home")"/>
           </transport>
           <schema data="Data" total="Total">
           </schema>
       </datasource>
   </kendo-autocomplete>
   ```

## Sending Parameters to the Server

The following example demonstrates how to configure the AutoComplete to send parameters to the server.

```Razor HtmlHelper
    @(Html.Kendo().AutoComplete()
        .Name("productAutoComplete")
        .DataTextField("ProductName") // Specify which property of the Product will be used by the AutoComplete.
        .DataSource(source =>
        {
            source.Read(read =>
            {
                read.Action("GetProducts", "Home")
                    .Data("onAdditionalData");
            });
        })
    )
```

```Razor TagHelper
<kendo-autocomplete name="productAutoComplete"
                    datatextfield="ProductName">
    <datasource>
        <transport>
            <read url=@Url.Action("GetProducts", "Home") data="onAdditionalData" />
        </transport>
    </datasource>

</kendo-autocomplete>
```

```JS script
    <script>
        function onAdditionalData() {
            return {
                text: $("#productAutoComplete").val()
            };
        }
    </script>
```

The following example demonstrates how the `GetProducts` method is used.

```C#
public JsonResult GetProducts(string text)
{
    var northwind = new SampleEntities();

    var products = northwind.Products.Select(product => new ProductViewModel
            {
            ProductID = product.ProductID,
            ProductName = product.ProductName,
            UnitPrice = product.UnitPrice ?? 0,
            UnitsInStock = product.UnitsInStock ?? 0,
            UnitsOnOrder = product.UnitsOnOrder ?? 0,
            Discontinued = product.Discontinued
            });

    if (!string.IsNullOrEmpty(text))
    {
        products = products.Where(p => p.ProductName.Contains(text));
    }

    return Json(products);
}
```

## See Also

* [Custom Binding by the AutoComplete HtmlHelper for ASP.NET Core (Demo)](https://demos.telerik.com/aspnet-core/autocomplete/custom-datasource)
* [Server-Side API](https://www.telerik.com/aspnet-core-ui/documentation/api/autocomplete)
