Value binding in AutoComplete Component

7 Jun 20243 minutes to read

Value binding in the AutoComplete control allows you to associate data values with each list item. This facilitates managing and retrieving selected values efficiently. The AutoComplete component provides flexibility in binding both primitive data types and complex objects.

Primitive Data Types

The AutoComplete control provides flexible binding capabilities for primitive data types like strings and numbers. You can effortlessly bind local primitive data arrays, fetch and bind data from remote sources, and even custom data binding to suit specific requirements. Bind the value of primitive data to the value property of the AutoComplete.

Primitive data types include:

  • String
  • Number
  • Boolean
  • Null

The following sample shows the example for preselect values for primitive data type

<div class="control-wrapper">
    <div id="default" style='padding-top:75px;margin:0 auto;width:250px;'>
        <ejs-autocomplete id="records" dataSource="@ViewBag.data" placeholder="e.g. Item 1" allowFiltering="true" value="@ViewBag.value" popupheight="200px">
        </ejs-autocomplete>
    </div>
</div>

Object Data Types

In the AutoComplete control, object binding allows you to bind to a dataset of objects. When allowObjectBinding is enabled, the value of the control will be an object of the same type as the selected item in the value property. This feature seamlessly binds arrays of objects, whether sourced locally, retrieved from remote endpoints, or customized to suit specific application needs.

The following sample shows the example for preselect values for object data type

@page
@model AutoCompleteBindingSamples.Pages.ObjectDataType

@{
    var objectType = new Record().RecordModelList();
    var value = new { ID = "id5", Text = "Item 5" };
}

<div class="control-wrapper">
    <div id="default" style='padding-top:75px;margin:0 auto;width:250px;'>
        <ejs-autocomplete id="records" dataSource="@objectType" placeholder="e.g. Item 1" allowFiltering="true" value="@value" allowObjectBinding="true" popupheight="200px">
            <e-autocomplete-fields value="Text" ></e-autocomplete-fields>
        </ejs-autocomplete>
    </div>
</div>
...
public class Record
{
    public string ID { get; set; }
    public string Text { get; set; }
    public List<Record> RecordList { set; get; }
    public List<Record> RecordModelList()
    {
        return Enumerable.Range(1, 150).Select(i => new Record()
        {
            ID = i.ToString(),
            Text = "Item " + i,
        }).ToList();
    }
}