# Implementing AutoComplete with Server Filtering in the Filter Component

## Environment

<table>
 <tr>
  <td>Product</td>
  <td>Telerik UI for ASP.NET MVC Filter</td>
 </tr>
 <tr>
  <td>Progress Telerik UI for ASP.NET MVC version</td>
  <td>Created with the 2023.3.1114 version</td>
 </tr>
</table>

## Description

How can I define the [AutoComplete](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/editors/autocomplete/overview) component with server filtering as a custom editor of a specified Telerik UI for ASP.NET MVC Filter field?

## Solution

1. Define a field within the [`Fields()`](https://docs.telerik.com/aspnet-mvc/api/kendo.mvc.ui.fluent/filterbuilder#fieldssystemaction) configuration and use the `EditorTemplateHandler()` option to specify the JavaScript function that will return the AutoComplete editor.

   ```Razor HtmlHelper
       @(Html.Kendo().Filter<Kendo.Mvc.Examples.Models.ProductViewModel>()
           .Name("filter")
           .ApplyButton(true)
           .DataSource("dataSource1")
           .Fields(f =>
           {
               f.Add(p=>p.ProductName).Label("Product Name").EditorTemplateHandler("productNameAutoCompleteEditor");
           })
       )
   ```

2. Initialize the AutoComplete editor within the <b>productNameAutoCompleteEditor</b> handler.

3. Handle the [`filtering`](https://docs.telerik.com/kendo-ui/api/ui/autocomplete/events/filtering) event of the AutoComplete, extract the search entry value from the event data, and store it into a global variable <b>searchParameter</b>.

4. Use the [`data`](https://docs.telerik.com/kendo-ui/api/data/datasource/configuration/transport.read#transportreaddata) option of the DataSource to pass the global variable as an additional parameter through the `Read` request of the AutoComplete. This way, you will ensure that the correct search entry will be sent to the server when the Filter component has multiple fields that use the same AutoComplete editor.

   ```JS scripts
       function productNameAutoCompleteEditor(container, options) {
           var searchParameter = "";
           $('<input data-bind="value: value" name="' + options.field + '"/>')
           .appendTo(container)
           .kendoAutoComplete({
               minLength: 3,
               placeholder: "Please type at least 3 characters.",
               filter: "contains",
               filtering: function(e) {
                   searchParameter = e.filter.value; // Get the search entry from the event data.
               },
               dataTextField: "ProductName",
               dataSource: {
                   serverFiltering: true,
                   transport: {
                       read: {
                           url: '@Url.Action("GetItems","Home")',
                           data: function () {
                               return {
                                   search: searchParameter
                               };
                           }
                       }
                   }
               }
           });
       }
   ```

   ```C# Controller
       public JsonResult GetItems(string search)
       {
           using (var northwind = GetContext())
           {
               var products = northwind.Products.Select(product => new ProductViewModel
               {
                   ProductID = product.ProductID,
                   ProductName = product.ProductName
               });

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

               return Json(products);
           }
       }
   ```

For a runnable example based on the code above, refer to the [REPL example on adding AutoComplete editor with server filtering in the Filter](https://netcorerepl.telerik.com/QHbFdEOi59tNg7zw17).

## More ASP.NET MVC Filter Resources

* [ASP.NET MVC Filter Documentation](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/data-management/filter/overview)

* [ASP.NET MVC Filter Demos](https://demos.telerik.com/aspnet-mvc/filter)

* [ASP.NET MVC Filter Product Page](https://www.telerik.com/aspnet-mvc/filter)

* [Telerik UI for ASP.NET MVC Video Onboarding Course (Free for trial users and license holders)](https://www.telerik.com/aspnet-mvc/documentation/getting-started/video-onboarding)

* [Telerik UI for ASP.NET MVC Forums](https://www.telerik.com/forums/aspnet-mvc)

## See Also

* [Client-Side API Reference of the Filter for ASP.NET MVC](https://docs.telerik.com/kendo-ui/api/ui/filter)

* [Server-Side API Reference of the Filter for ASP.NET MVC](https://docs.telerik.com/aspnet-mvc/api/filter)

* [Telerik UI for ASP.NET MVC Breaking Changes](https://www.telerik.com/aspnet-mvc/documentation/backwards-compatibility/2023-backwards-compatibility)

* [Telerik UI for ASP.NET MVC Knowledge Base](https://docs.telerik.com/aspnet-mvc/knowledge-base)
