ASP.NET Core Blazor [Preview]
Search results

Getting Started

This section briefly explains about how to include a simple AutoComplete in your ASP.NET Core Razor application. You can refer ASP.NET Core Razor Getting Started documentation page for introduction part of the system requirements and configure the common specifications.

Adding component package to the application

Open ~/_Imports.razor file and import the Syncfusion.EJ2.RazorComponents.DropDowns packages.

@using Syncfusion.EJ2.RazorComponents

@using Syncfusion.EJ2.RazorComponents.DropDowns

Adding AutoComplete component to the application

AutoComplete component can be rendered by using the EjsAutoComplete tag helper in ASP.NET Core Razor application. Add the Syncfusion Essential JS 2 AutoComplete component in any web page razor in the Pages folder. For example, the AutoComplete component is added in the ~/Pages/Index.razor page.

The following example shows a basic AutoComplete component.

<EjsAutoComplete id="autocomplete" placeholder="e.g. Australia"></EjsAutoComplete>

Run the application

After successful compilation of your application, simply press F5 to run the application.

Output be like the below.

AutoComplete

Binding data source

After initialization, populate the AutoComplete with data using the dataSource property. Here, an array of string values is passed to the AutoComplete component.

The following example illustrates the output in your browser.

<EjsAutoComplete ID="country" PlaceHolder="Select a country"  DataSource="@localdata">
<AutoCompleteFieldSettings value="Name"></AutoCompleteFieldSettings>
</EjsAutoComplete>

@functions {

    public class countries
    {
        public string Name { get; set; }
        public string Code { get; set; }
    }

    List<countries> localdata = new List<countries> {
        new countries() { Name = "Australia", Code = "AU" },
        new countries() { Name = "Bermuda", Code = "BM" },
        new countries() { Name = "Canada", Code = "CA" },
        new countries() { Name = "Cameroon", Code = "CM" },
        new countries() { Name = "Denmark", Code = "DK" },
        new countries() { Name = "France", Code = "FR" },
        new countries() { Name = "Finland", Code = "FI" },
        new countries() { Name = "Germany", Code = "DE" },
        new countries() { Name = "Greenland", Code = "GL" },
        new countries() { Name = "Hong Kong", Code = "HK" },
        new countries() { Name = "India", Code = "IN" },
        new countries() { Name = "Italy", Code = "IT" },
        new countries() { Name = "Japan", Code = "JP" },
        new countries() { Name = "Mexico", Code = "MX" },
        new countries() { Name = "Norway", Code = "NO" },
        new countries() { Name = "Poland", Code = "PL" },
        new countries() { Name = "Switzerland", Code = "CH" },
        new countries() { Name = "United Kingdom", Code = "GB" },
        new countries() { Name = "United States", Code = "US" },
    };
}

Custom values

The AutoComplete allows the user to give input as custom value which is not required to present in predefined set of values. By default, this support is enabled by AllowCustom property. The custom value will be sent to post back handler when a form is about to be submitted.

<EjsAutoComplete ID="country" PlaceHolder="Select a country" AllowCustom="true" DataSource="@localdata">
    <AutoCompleteFieldSettings value="Name"></AutoCompleteFieldSettings>
</EjsAutoComplete>

@functions {

    public class countries
    {
        public string Name { get; set; }
        public string Code { get; set; }
    }

    List<countries> localdata = new List<countries> {
        new countries() { Name = "Australia", Code = "AU" },
        new countries() { Name = "Bermuda", Code = "BM" },
        new countries() { Name = "Canada", Code = "CA" },
        new countries() { Name = "Cameroon", Code = "CM" },
        new countries() { Name = "Denmark", Code = "DK" },
        new countries() { Name = "France", Code = "FR" },
        new countries() { Name = "Finland", Code = "FI" },
        new countries() { Name = "Germany", Code = "DE" },
        new countries() { Name = "Greenland", Code = "GL" },
        new countries() { Name = "Hong Kong", Code = "HK" },
        new countries() { Name = "India", Code = "IN" },
        new countries() { Name = "Italy", Code = "IT" },
        new countries() { Name = "Japan", Code = "JP" },
        new countries() { Name = "Mexico", Code = "MX" },
        new countries() { Name = "Norway", Code = "NO" },
        new countries() { Name = "Poland", Code = "PL" },
        new countries() { Name = "Switzerland", Code = "CH" },
        new countries() { Name = "United Kingdom", Code = "GB" },
        new countries() { Name = "United States", Code = "US" },
    };
}

Configure the suggestion list

By default, suggestion list width automatically adjusts according to the AutoComplete input element’s width, and the height of the suggestion list has 300px.

The height and width of the popup list can also be customized using the PopupHeight and PopupWidth property respectively.

In the following sample, suggestion list’s width and height are configured.

<EjsAutoComplete ID="country" PlaceHolder="Select a country" AllowCustom="true" DataSource="@localdata" PopupHeight="200px" PopupWidth="200px">
    <AutoCompleteFieldSettings value="Name"></AutoCompleteFieldSettings>
</EjsAutoComplete>

@functions {

    public class countries
    {
        public string Name { get; set; }
        public string Code { get; set; }
    }

    List<countries> localdata = new List<countries> {
        new countries() { Name = "Australia", Code = "AU" },
        new countries() { Name = "Bermuda", Code = "BM" },
        new countries() { Name = "Canada", Code = "CA" },
        new countries() { Name = "Cameroon", Code = "CM" },
        new countries() { Name = "Denmark", Code = "DK" },
        new countries() { Name = "France", Code = "FR" },
        new countries() { Name = "Finland", Code = "FI" },
        new countries() { Name = "Germany", Code = "DE" },
        new countries() { Name = "Greenland", Code = "GL" },
        new countries() { Name = "Hong Kong", Code = "HK" },
        new countries() { Name = "India", Code = "IN" },
        new countries() { Name = "Italy", Code = "IT" },
        new countries() { Name = "Japan", Code = "JP" },
        new countries() { Name = "Mexico", Code = "MX" },
        new countries() { Name = "Norway", Code = "NO" },
        new countries() { Name = "Poland", Code = "PL" },
        new countries() { Name = "Switzerland", Code = "CH" },
        new countries() { Name = "United Kingdom", Code = "GB" },
        new countries() { Name = "United States", Code = "US" },
    };
}

Output be like the below.

AutoComplete