# Getting Started with the AutoComplete

This tutorial explains how to set up a basic Telerik UI for ASP.NET MVC AutoComplete and highlights the major steps in the configuration of the component.

You will initialize a AutoComplete component and load it with data.

![Sample Telerik UI for ASP.NET MVC AutoComplete](https://www.telerik.com/aspnet-mvc/documentation/assets/bda9ec4adfe850c42b81d5ca307d4bb1/autocomplete-getting-started.png)

## Prerequisites

To successfully complete the tutorial, you need a project that is already configured to use the Telerik UI for ASP.NET MVC components:

* To create a new pre-configured project for the Telerik UI for ASP.NET MVC components, you can [use a project template](https://www.telerik.com/aspnet-mvc/documentation/getting-started-mvc/first-steps).

* To manually configure an existing project by using NuGet, see the [Adding Telerik UI through NuGet](https://www.telerik.com/aspnet-mvc/documentation/getting-started/setup-with-nuget).

## 1. Prepare the CSHTML File

The first step is to add the required directives at the top of the `.cshtml` document:

* To use the Telerik UI for ASP.NET MVC HtmlHelpers:

  ```cshtml
  @using Kendo.Mvc.UI
  ```

You will also add some sample data that the AutoComplete will display. Optionally, you can structure the content in the view by adding the desired HTML elements like headings, divs, paragraphs, and others.

```Razor HtmlHelper
    @using Kendo.Mvc.UI

    @{
        var continents = new List<SelectListItem> {
            new SelectListItem() {Text = "Africa", Value = "1"},
            new SelectListItem() {Text = "Europe", Value = "2"},
            new SelectListItem() {Text = "Asia", Value = "3"},
            new SelectListItem() {Text = "North America", Value = "4"},
            new SelectListItem() {Text = "South America", Value = "5"},
            new SelectListItem() {Text = "Antarctica", Value = "6"},
            new SelectListItem() {Text = "Australia", Value = "7"}
        };
    }

    <h4>AutoComplete:</h4>
```

## 2. Initialize the AutoComplete

Use the AutoComplete HtmlHelper  to add the component to the page:

* The `Name()` configuration method is mandatory as its value is used for the `id` and the `name` attributes of the AutoComplete element.
* The `Placeholder()` configuration specifies the text that is shown initially within the AutoComplete, when none of the items is selected.
* The `DataTextField()` option specifies the name of the field in the data, which will be used to show text in the items. The same field is used to set the value of the component.

```Razor HtmlHelper
    @using Kendo.Mvc.UI

    @{
        var continents = new List<SelectListItem> {
            new SelectListItem() {Text = "Africa", Value = "1"},
            new SelectListItem() {Text = "Europe", Value = "2"},
            new SelectListItem() {Text = "Asia", Value = "3"},
            new SelectListItem() {Text = "North America", Value = "4"},
            new SelectListItem() {Text = "South America", Value = "5"},
            new SelectListItem() {Text = "Antarctica", Value = "6"},
            new SelectListItem() {Text = "Australia", Value = "7"}
        };
    }

    <h4>AutoComplete:</h4>

	@(Html.Kendo().AutoComplete()
		.Name("autocomplete")
		.DataTextField("Text")
		.Placeholder("Select a continent")
		.HtmlAttributes(new { style = "width: 300px;"})
		.BindTo(continents)
	)
```

> Do not set the `Name()` option when using `AutoCompleteFor`. The `[ComponentName]For` method automatically sets the control's `Name()` to the field it is bound to. For more information, see the [Fundamentals](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/helper-basics/fundamentals-mvc) article.

## 3. Select a Default Value

The next step is to set one of the items of the AutoComplete as selected. The following example shows how to use the `.Value()` method of the AutoComplete to select the item with text `Europe`.

```Razor HtmlHelper
    @using Kendo.Mvc.UI

    @{
        var continents = new List<SelectListItem> {
            new SelectListItem() {Text = "Africa", Value = "1"},
            new SelectListItem() {Text = "Europe", Value = "2"},
            new SelectListItem() {Text = "Asia", Value = "3"},
            new SelectListItem() {Text = "North America", Value = "4"},
            new SelectListItem() {Text = "South America", Value = "5"},
            new SelectListItem() {Text = "Antarctica", Value = "6"},
            new SelectListItem() {Text = "Australia", Value = "7"}
        };
    }

    <h4>AutoComplete:</h4>

	@(Html.Kendo().AutoComplete()
		.Name("autocomplete")
		.DataTextField("Text")
		.Placeholder("Select a continent")
		.HtmlAttributes(new { style = "width: 300px;"})
		.BindTo(continents)
        .Value("Europe")
	)
```

## 4. Handle an AutoComplete Event

The AutoComplete component exposes convenient events for implementing your desired logic. In this example, you will use the `Change()` event to log the value of the selected item in the browser's console. You will also handle the `Open()` event and log an entry in the browser's console when the event fires.

```Razor HtmlHelper
    @using Kendo.Mvc.UI

    @{
        var continents = new List<SelectListItem> {
            new SelectListItem() {Text = "Africa", Value = "1"},
            new SelectListItem() {Text = "Europe", Value = "2"},
            new SelectListItem() {Text = "Asia", Value = "3"},
            new SelectListItem() {Text = "North America", Value = "4"},
            new SelectListItem() {Text = "South America", Value = "5"},
            new SelectListItem() {Text = "Antarctica", Value = "6"},
            new SelectListItem() {Text = "Australia", Value = "7"}
        };
    }

    <script>
        function onChange(e) {
            console.log("Change :: " + this.value());
        }

        function onOpen(e) {
            console.log("AutoComplete opened");
        }
    </script>

    <h4>AutoComplete:</h4>

    @(Html.Kendo().AutoComplete()
		.Name("autocomplete")
		.DataTextField("Text")
		.Placeholder("Select a continent")
		.HtmlAttributes(new { style = "width: 300px;"})
		.BindTo(continents)
        .Value("Europe")
        .Events(e => e // Configure the client-side events.
            .Change("onChange")
            .Open("onOpen")
        )
	)
```

## 5. (Optional) Reference Existing AutoComplete Instances

To use the client-side API of the AutoComplete and build on top of its initial configuration, you need a reference to the AutoComplete instance. Once you get a valid reference, you can call the respective API methods:

1. Use the `.Name()` (id attribute) of the component instance to get a reference.

   ```JS script
       <script>
           $(document).ready(function() {
               var autocompleteReference = $("#autocomplete").data("kendoAutoComplete"); // autocompleteReference is a reference to the existing AutoComplete instance of the helper.
           })
       </script>
   ```

2. Use the [AutoComplete client-side API](https://docs.telerik.com/kendo-ui/api/ui/autocomplete#methods) to control the behavior of the control. In this example, you will use the `value` method to select an item.

   ```JS script
       <script>
           $(document).ready(function() {
               var autocompleteReference = $("#autocomplete").data("kendoAutoComplete"); // autocompleteReference is a reference to the existing AutoComplete instance of the helper.
               autocompleteReference.value("Asia"); 
           })
       </script>
   ```

## Next Steps

* [Binding the AutoComplete to Data](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/editors/autocomplete/binding/binding-overview)
* [Customize the Appearance of the AutoComplete](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/editors/autocomplete/appearance)
* [Controlling the Rendering of the AutoComplete through Templates](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/editors/autocomplete/templates)

## See Also

* [Using the API of the AutoComplete for ASP.NET MVC (Demo)](https://demos.telerik.com/aspnet-mvc/autocomplete/api)
* [Client-Side API of the AutoComplete ](https://docs.telerik.com/kendo-ui/api/ui/autocomplete)
* [Server-Side API of the AutoComplete](https://www.telerik.com/aspnet-mvc/documentation/api/autocomplete)
* [Knowledge Base Section](https://www.telerik.com/aspnet-mvc/documentation/knowledge-base)
