Example of Data Binding in ASP.NET MVC MultiSelect Control
This sample demonstrates the different data binding supports of the MultiSelect. Click the MultiSelect element and choose one or more items from the suggestion list. At the very first time, when clicked on the remote data MultiSelect, the loader icon will be shown until the remote request get the data from server and display it.
@Html.UiMultiSelect("local", new UiMultiSelectOptions
{
Placeholder = "Select countries",
DataSource = (IEnumerable<DropdownsDemoController.CountriesData>)ViewData["localdata"],
Fields = new UiDropDownFields { Text = "Name", Value = "Code" },
Value = (string[])ViewData["val"]
})
@Html.UiMultiSelect("remote", new UiMultiSelectOptions
{
Placeholder = "Select names",
SortOrder = (UiDropDownSortOrder)ViewData["sort"],
DataUrl = Url.Action("EmployeeData", "DropdownsDemo"),
Fields = new UiDropDownFields { Text = "FirstName", Value = "EmployeeID" },
Value = (string[])ViewData["val"]
})
<style>
.control-section .content { margin: 0 auto; width: 250px; }
.control-section h4 { margin: 0 0 8px; }
</style>
@using Lumen.UI.Mvc.Demo.Controllers
<div class="control-section">
<div class="content" style="padding-top:15px">
<h3> Local Data</h3>
@Html.UiMultiSelect("local", new UiMultiSelectOptions
{
Placeholder = "Select countries",
DataSource = (IEnumerable<DropdownsDemoController.CountriesData>)ViewData["localdata"],
Fields = new UiDropDownFields { Text = "Name", Value = "Code" },
Value = (string[])ViewData["val"]
})
<h3 style="margin-top:24px">Remote Data</h3>
@Html.UiMultiSelect("remote", new UiMultiSelectOptions
{
Placeholder = "Select names",
SortOrder = (UiDropDownSortOrder)ViewData["sort"],
DataUrl = Url.Action("EmployeeData", "DropdownsDemo"),
Fields = new UiDropDownFields { Text = "FirstName", Value = "EmployeeID" },
Value = (string[])ViewData["val"]
})
</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 MultiSelectController : Controller
{
// The page is Views/MultiSelect/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 MultiSelect 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 MultiSelect, will require the following minimal information to interact with 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 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.