Example of Data Binding in ASP.NET MVC DropDownList Control
This sample demonstrates the different data binding supports of the DropDownList. Click the DropDownList element and select an item from the suggestion list. At the very first time, when you click on the remote data DropDownList, the loader icon will be shown until the remote request get the data from the server and display it.
Remote Data
@Html.UiDropDownList("games", new UiDropDownListOptions
{
Placeholder = "Select a game",
PopupHeight = "200px",
DataSource = (IEnumerable<DropdownsDemoController.GameList>)ViewData["localdata"],
Fields = new UiDropDownFields { Text = "Game" }
})
@Html.UiDropDownList("customers", new UiDropDownListOptions
{
Placeholder = "Select a name",
PopupHeight = "200px",
DataUrl = Url.Action("EmployeeData", "DropdownsDemo"),
Fields = new UiDropDownFields { Text = "FirstName", Value = "EmployeeID" },
SortOrder = (UiDropDownSortOrder)ViewData["sort"]
})
<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.UiDropDownList("games", new UiDropDownListOptions
{
Placeholder = "Select a game",
PopupHeight = "200px",
DataSource = (IEnumerable<DropdownsDemoController.GameList>)ViewData["localdata"],
Fields = new UiDropDownFields { Text = "Game" }
})
</div>
</div>
<div id="remote-data" style="padding-top:15px">
<div class="content">
<h4>Remote Data</h4>
@Html.UiDropDownList("customers", new UiDropDownListOptions
{
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>
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 DropDownListController : Controller
{
// The page is Views/DropDownList/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 DropDownList loads the data either from the local data sources, or remote data services that is through the dataSource property. It supports the data type of array or DataManager.
The DataManager that act as an interface between service endpoint and DropDownList, will require the below 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.
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 the particular service endpoints. They are:
UrlAdaptor- Use this to interact with 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.