Example of Data Binding in ASP.NET MVC AutoComplete Control
This sample demonstrates the different data binding supports of the AutoComplete. Type a character(s) in the AutoComplete element and the remaining characters are automatically filled based on the first matched item. Also, provided option to enable/disable this autofill feature in the property panel.
Remote Data
@Html.UiAutoComplete("country", new UiAutoCompleteOptions
{
DataSource = (IEnumerable<DropdownsDemoController.CountriesData>)ViewData["localdata"],
Placeholder = "e.g. Australia",
SortOrder = (UiDropDownSortOrder)ViewData["sort"],
Autofill = true,
FilterType = UiDropDownFilterType.StartsWith,
Fields = new UiDropDownFields { Text = "Name" }
})
@Html.UiAutoComplete("products", new UiAutoCompleteOptions
{
Placeholder = "e.g. Andrew Fuller",
Autofill = true,
SuggestionCount = 5,
FilterType = UiDropDownFilterType.StartsWith,
DataUrl = Url.Action("EmployeeData", "DropdownsDemo"),
Fields = new UiDropDownFields { Text = "FirstName", Value = "EmployeeID" }
})
@Html.UiCheckBox("checkAutofill", true, new UiCheckBoxOptions { Label = "Autofill", OnChange = "change" })
<style>
.control-section .content { margin: 0 auto; width: 250px; }
</style>
@using Lumen.UI.Mvc.Demo.Controllers
<div class="control-section">
<div id="local-data" style="padding-top:15px">
<div class="content">
<h4> Local Data</h4>
@Html.UiAutoComplete("country", new UiAutoCompleteOptions
{
DataSource = (IEnumerable<DropdownsDemoController.CountriesData>)ViewData["localdata"],
Placeholder = "e.g. Australia",
SortOrder = (UiDropDownSortOrder)ViewData["sort"],
Autofill = true,
FilterType = UiDropDownFilterType.StartsWith,
Fields = new UiDropDownFields { Text = "Name" }
})
</div>
</div>
<div id="remote-data" style="padding-top:15px">
<div class="content">
<h4>Remote Data</h4>
@Html.UiAutoComplete("products", new UiAutoCompleteOptions
{
Placeholder = "e.g. Andrew Fuller",
Autofill = true,
SuggestionCount = 5,
FilterType = UiDropDownFilterType.StartsWith,
DataUrl = Url.Action("EmployeeData", "DropdownsDemo"),
Fields = new UiDropDownFields { Text = "FirstName", Value = "EmployeeID" }
})
</div>
</div>
</div>
<div class="demo-property-panel" title="Properties">
<div class="demo-property-row">
@Html.UiCheckBox("checkAutofill", true, new UiCheckBoxOptions { Label = "Autofill", OnChange = "change" })
</div>
</div>
<script>
// Syncfusion: the autofill property of both AutoCompletes is the state of the check box.
function change(checkedValue) {
window.Lumen.get('country').setAutofill(checkedValue);
window.Lumen.get('products').setAutofill(checkedValue);
}
</script>
using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class AutoCompleteController : Controller
{
// The page is Views/AutoComplete/DataBinding.cshtml; it draws what ViewData holds.
public ActionResult DataBinding()
{
ViewData["localdata"] = new CountriesData().CountriesList();
ViewData["sort"] = UiDropDownSortOrder.Ascending;
return View();
}
/// <summary>A row of the Employees data.</summary>
public class EmployeeRow
{
public int EmployeeID { get; set; }
public string FirstName { get; set; }
public string Designation { get; set; }
public string Country { get; set; }
}
/// <summary>DataUrl of the remote samples of the Dropdowns: the employees, with the typed text as a "where" on FirstName.</summary>
[HttpPost]
[ValidateJsonAntiForgeryToken]
public ActionResult EmployeeData(UiDataRequest request)
{
return Json(UiDataOperations.Execute(EmployeeRows.AsQueryable(), request, "FirstName"));
}
private static readonly List<EmployeeRow> EmployeeRows = new List<EmployeeRow>
{
new EmployeeRow { EmployeeID = 1, FirstName = "Andrew Fuller", Designation = "Team Lead", Country = "England" },
new EmployeeRow { EmployeeID = 2, FirstName = "Anne Dodsworth", Designation = "Developer", Country = "USA" },
new EmployeeRow { EmployeeID = 3, FirstName = "Janet Leverling", Designation = "HR", Country = "USA" },
// ... 406 more rows
};
public class CountriesData
{
public string Name { get; set; }
public string Code { get; set; }
public List<CountriesData> CountriesList()
{
List<CountriesData> country = new List<CountriesData>();
country.Add(new CountriesData { Name = "Australia", Code = "AU" });
country.Add(new CountriesData { Name = "Bermuda", Code = "BM" });
country.Add(new CountriesData { Name = "Canada", Code = "CA" });
// ... 16 more rows
return country;
}
}
}
}
@* Install once: Install-Package Lumen.UI.Mvc (or: dotnet add package Lumen.UI.Mvc) *@
@* Views/Web.config: <add namespace="Lumen.UI.Mvc" /> *@
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/Content/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@RenderSection("scripts", required: false)
</body>
@* Dropdowns with a DataUrl: @Html.AntiForgeryToken() on the page and [HttpPost, ValidateJsonAntiForgeryToken] on the action. *@
The AutoComplete loads the data either from local data sources or remote data services through the dataSource property. It supports the data type of array or DataManager.
The DataManager, that act as an interface between service endpoint and AutoComplete, will require the follwoing minimal information to interact with the service endpoint properly.
DataManager->url- Defines the service endpoint to fetch data.-
DataManager->adaptor- Defines the adaptor option. By default,ODataAdaptoris used for remote binding.
The adaptor is responsible for processing response and request from/to the service endpoint. syncfusion/ej2-data package provides some predefined adaptors that are designed to interact with particular service endpoints. They are:
UrlAdaptor- Use this to interact any remote services.ODataAdaptor- Use this to interact with OData endpoints.ODataV4Adaptor- Use this to interact with OData V4 endpoints.WebApiAdaptor- Use this to interact with Web API created under OData standards.WebMethodAdaptor- Use this to interact with web methods.
In this sample, the local data is bound to a collection of sports data and the remote data is bound to a collection of customer data as an instance of DataManager. Also, provided option to enable/disable autofill feature in the property panel. StartsWith filter type is set in this sample for showcase the autofill behavior.