Example of Virtualization in ASP.NET MVC DropDownList Control
This example demonstrates the virtualization support of the DropDownList in ASP.NET MVC application. The component has 150 items bound to it; however, when you open the suggestion list, only few items are loaded based on the popup height, and the remaining items are loaded while scrolling.
Remote Data
Grouping
@Html.UiDropDownList("local", new UiDropDownListOptions { Placeholder = "e.g Item 1", PopupHeight = "200px", DataSource = Model.RecordList, EnableVirtualization = true, AllowFiltering = true, Fields = new UiDropDownFields { Text = "Text", Value = "ID" } })
@Html.UiDropDownList("remote", new UiDropDownListOptions { Placeholder = "Id", PopupHeight = "200px", DataUrl = Url.Action("Records", "DropdownsDemo"), EnableVirtualization = true, Fields = new UiDropDownFields { Text = "Id", Value = "Id" } })
@Html.UiDropDownList("group", new UiDropDownListOptions { Placeholder = "e.g Item 1", PopupHeight = "200px", DataSource = Model.RecordList, EnableVirtualization = true, AllowFiltering = true, Fields = new UiDropDownFields { GroupBy = "Group", Text = "Text", Value = "ID" } })
<style>
.control-section .content { margin: 0 auto; width: 250px; }
</style>
@using Lumen.UI.Mvc.Demo.Controllers
@model DropdownsDemoController.DropDownListRecord
<div class="control-section">
<div id="local-data" style="padding-top:15px">
<div class="content">
<h4>Local Data</h4>
@Html.UiDropDownList("local", new UiDropDownListOptions { Placeholder = "e.g Item 1", PopupHeight = "200px", DataSource = Model.RecordList, EnableVirtualization = true, AllowFiltering = true, Fields = new UiDropDownFields { Text = "Text", Value = "ID" } })
</div>
</div>
<div id="remote-data" style="padding-top:15px">
<div class="content">
<h4>Remote Data</h4>
@Html.UiDropDownList("remote", new UiDropDownListOptions { Placeholder = "Id", PopupHeight = "200px", DataUrl = Url.Action("Records", "DropdownsDemo"), EnableVirtualization = true, Fields = new UiDropDownFields { Text = "Id", Value = "Id" } })
</div>
</div>
<div id="group-data" style="padding-top:15px">
<div class="content">
<h4>Grouping</h4>
@Html.UiDropDownList("group", new UiDropDownListOptions { Placeholder = "e.g Item 1", PopupHeight = "200px", DataSource = Model.RecordList, EnableVirtualization = true, AllowFiltering = true, Fields = new UiDropDownFields { GroupBy = "Group", Text = "Text", Value = "ID" } })
</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/VirtualScroll.cshtml; it draws what ViewData holds.
public ActionResult VirtualScroll()
{
DropDownListRecord model = new DropDownListRecord();
model.RecordList = new DropDownListRecord().RecordModelList();
return View(model);
}
public class DropDownListRecord
{
public string ID { get; set; }
public string Text { get; set; }
public string Group { get; set; }
public List<DropDownListRecord> RecordList { set; get; }
public List<DropDownListRecord> RecordModelList()
{
return Enumerable.Range(1, 150).Select(i => new DropDownListRecord()
{
ID = i.ToString(),
Text = "Item " + i,
Group = "Group " + (char)('A' + (i - 1) % 4)
}).ToList();
}
}
/// <summary>A row of the virtual-scrolling samples.</summary>
public class RecordInfo
{
public int Id { get; set; }
public string Text { get; set; }
}
private static readonly List<RecordInfo> RecordList = Enumerable.Range(1, 1000).Select(i => new RecordInfo { Id = i, Text = "Record " + i }).ToList();
/// <summary>DataUrl of the virtual-scrolling sample: 1,000 records, read a page at a time (skip, take, requiresCounts).</summary>
[HttpPost]
[ValidateJsonAntiForgeryToken]
public ActionResult Records(UiDataRequest request)
{
return Json(UiDataOperations.Execute(RecordList.AsQueryable(), request, "Id"));
}
}
}
@* 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 component supports virtualization, which improves UI performance for large amounts of data. To enable virtualization, set the enableVirtualization property to true. When virtualization is enabled, DropDownList doesn't render the entire suggestion data source on initial component rendering. It loads the N number of items in the popup on initial rendering and the remaining set number of items will load while scrolling. Virtualization works with both local and remote data.