Example of Virtualization in ASP.NET MVC ListView Control
This sample demonstrates the default functionalities of UI virtualization. Scroll list item to experience UI virtualization.
${Initial}${Text}
Contacts
Load data
Time taken 0 ms
@Html.UiListView(new UiListViewOptions { Id = "ui-list", EnableVirtualization = true, ShowHeader = true, HeaderTitle = "Contacts", Height = "500px", Template = "#listTemplate", Items = (List<UiListItem>)ViewData["listData"] })
@Html.UiDropDownList("ddl", new UiDropDownListOptions { Placeholder = "Select a range", Width = "120px", Value = "1", OnChange = "onChange", Items = (List<UiListItem>)ViewData["ddlData"] })
<style>
.control-section { max-width: 700px; margin: 0 auto; padding: 10px 20px; }
.control-section h4 { margin: 14px 0 6px; }
#flat-list, #group-list { display: inline-block; vertical-align: top; width: 48%; min-width: 260px; margin-right: 1%; }
.lv-row { display: grid; grid-template-columns: 44px 1fr; column-gap: 12px; align-items: center; width: 100%; box-sizing: border-box; padding: 6px 4px; }
.lv-contact { grid-template-columns: 44px 1fr; }
.lv-avatar { display: inline-flex; width: 36px; height: 36px; align-items: center; justify-content: center; border-radius: 50%; background: var(--ui-row-hover-bg); font-size: 14px; font-weight: 600; flex: none; }
.property-row { display: flex; gap: 12px; align-items: center; padding: 8px 0; }
.property-row .userselect { min-width: 90px; }
</style>
<template id="listTemplate"><div class="lv-row lv-contact"><span class="lv-avatar">${Initial}</span><span class="lv-name">${Text}</span></div></template>
<div class="control-section">
@Html.UiListView(new UiListViewOptions { Id = "ui-list", EnableVirtualization = true, ShowHeader = true, HeaderTitle = "Contacts", Height = "500px", Template = "#listTemplate", Items = (List<UiListItem>)ViewData["listData"] })
</div>
<div class="demo-property-panel property-section" title="Properties">
<div class="property-row"><span class="userselect">Load data</span> @Html.UiDropDownList("ddl", new UiDropDownListOptions { Placeholder = "Select a range", Width = "120px", Value = "1", OnChange = "onChange", Items = (List<UiListItem>)ViewData["ddlData"] })</div>
<div class="property-row"><span class="userselect">Time taken</span> <span id="time">0 ms</span></div>
</div>
<script>
var names = ["Nancy","Andrew","Janet","Margaret","Steven","Laura","Robert","Michael","Albert","Nolan"];
// Syncfusion: the list sets how many contacts are loaded, and the time it took is shown
function onChange(value) {
var count = { '1': 1010, '5': 5010, '10': 10010, '25': 25010 }[value];
if (!count) { return; }
var start = performance.now();
var data = [];
for (var i = 0; i < count; i++) {
var name = names[(i * 7 + (i >> 3)) % names.length];
data.push({ value: String(i), text: name, data: { Initial: name.charAt(0) } });
}
window.Lumen.get('ui-list').setItems(data);
document.getElementById('time').textContent = Math.round(performance.now() - start) + ' ms';
}
</script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class ListViewController : Controller
{
// The page is Views/ListView/Virtualization.cshtml; it draws what ViewData holds.
public ActionResult Virtualization()
{
string[] names = { "Nancy", "Andrew", "Janet", "Margaret", "Steven", "Laura", "Robert", "Michael", "Albert", "Nolan" };
List<UiListItem> contacts = new List<UiListItem>();
for (int i = 0; i < 1010; i++)
{
string name = names[(i * 7 + (i >> 3)) % names.Length];
contacts.Add(new UiListItem { Value = i.ToString(), Text = name, Data = new { Initial = name.Substring(0, 1) } });
}
ViewData["listData"] = contacts;
ViewData["ddlData"] = new List<UiListItem> { new UiListItem("1", "1k"), new UiListItem("5", "5k"), new UiListItem("10", "10k"), new UiListItem("25", "25k") };
return View();
}
}
}
@* 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>
UI virtualization is an optimization technique to avoid unnecessarily constructing and rendering objects for list items by loading only visible list items in a view port. This helps to improve ListView performance when loading a large number of items. The list items are updated dynamically while users scroll the list. The virtualization can be enabled by using the EnableVirtualization API in Listview.