Example of Data Binding in ASP.NET MVC ComboBox Control
This sample demonstrates the different data binding supports of the ComboBox. Type a character(s) in the ComboBox 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.UiComboBox("games", new UiComboBoxOptions
{
Placeholder = "Select a game",
Autofill = true,
PopupHeight = "230px",
DataSource = (IEnumerable<DropdownsDemoController.GameList>)ViewData["localdata"],
Fields = new UiDropDownFields { Text = "Game", Value = "Id" }
})
@Html.UiComboBox("customers", new UiComboBoxOptions
{
Autofill = true,
Placeholder = "Select a name",
PopupHeight = "200px",
DataUrl = Url.Action("EmployeeData", "DropdownsDemo"),
Fields = new UiDropDownFields { Text = "FirstName", Value = "EmployeeID" },
SortOrder = (UiDropDownSortOrder)ViewData["sort"]
})
@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.UiComboBox("games", new UiComboBoxOptions
{
Placeholder = "Select a game",
Autofill = true,
PopupHeight = "230px",
DataSource = (IEnumerable<DropdownsDemoController.GameList>)ViewData["localdata"],
Fields = new UiDropDownFields { Text = "Game", Value = "Id" }
})
</div>
</div>
<div id="remote-data" style="padding-top:15px">
<div class="content">
<h4>Remote Data</h4>
@Html.UiComboBox("customers", new UiComboBoxOptions
{
Autofill = true,
Placeholder = "Select a name",
PopupHeight = "200px",
DataUrl = Url.Action("EmployeeData", "DropdownsDemo"),
Fields = new UiDropDownFields { Text = "FirstName", Value = "EmployeeID" },
SortOrder = (UiDropDownSortOrder)ViewData["sort"]
})
</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 ComboBoxes is the state of the check box.
function change(checkedValue) {
window.Lumen.get('games').setAutofill(checkedValue);
window.Lumen.get('customers').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 ComboBoxController : Controller
{
// The page is Views/ComboBox/DataBinding.cshtml; it draws what ViewData holds.
public ActionResult DataBinding()
{
ViewData["localdata"] = new GameList().GameLists();
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 GameList
{
public string Id { get; set; }
public string Game { get; set; }
public List<GameList> GameLists()
{
List<GameList> game = new List<GameList>();
game.Add(new GameList { Id = "Game1", Game = "American Football" });
game.Add(new GameList { Id = "Game2", Game = "Badminton" });
game.Add(new GameList { Id = "Game3", Game = "Basketball" });
game.Add(new GameList { Id = "Game4", Game = "Cricket" });
game.Add(new GameList { Id = "Game5", Game = "Football" });
game.Add(new GameList { Id = "Game6", Game = "Golf" });
game.Add(new GameList { Id = "Game7", Game = "Hockey" });
game.Add(new GameList { Id = "Game8", Game = "Rugby" });
game.Add(new GameList { Id = "Game9", Game = "Snooker" });
game.Add(new GameList { Id = "Game10", Game = "Tennis" });
return game;
}
}
}
}
@* 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 ComboBox 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 ComboBox will require the following 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 which 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.