Example of Template in ASP.NET MVC ListBox Control
This sample demonstrates the Template of a ListBox. Here, SVG icons were used for visual representation of every list items
- JavaScript
- TypeScript
- Angular
- React
- Vue
@Html.UiListBox("listbox", new UiListBoxOptions { DataSource = (IEnumerable<object>)ViewData["data"], Fields = new UiDropDownFields { Value = "text", Text = "text" }, ItemTemplate = "#listItem" })
<style>
.control-section #listbox-control, .control-section #listbox-selection { margin: 0 auto; width: 300px; padding-top: 30px; }
.control-section h4 { margin: 0 0 8px; }
.list-wrapper { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 10px; padding: 6px 0; }
.list-wrapper .avatar { width: 48px; height: 48px; border-radius: 50%; background: #f3f2f1; flex: none; display: inline-flex; align-items: center; justify-content: center; }
.list-wrapper .avatar img { width: 28px; height: 28px; }
.list-wrapper .text { font-weight: 600; }
.list-wrapper .description { flex-basis: 100%; font-size: 12px; opacity: .7; }
</style>
<template id="listItem"><div class="list-wrapper"><span class="avatar"><img src="@Url.Content("~/Content/listbox/pic/")${pic}.svg" alt="pic"/></span><span class="text"> ${text} </span><span class="description"> ${description} </span></div></template>
<div class="control-section">
<div id="listbox-control">
@Html.UiListBox("listbox", new UiListBoxOptions { DataSource = (IEnumerable<object>)ViewData["data"], Fields = new UiDropDownFields { Value = "text", Text = "text" }, ItemTemplate = "#listItem" })
</div>
</div>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class ListBoxController : Controller
{
// The page is Views/ListBox/Template.cshtml; it draws what ViewData holds.
public ActionResult Template()
{
List<object> data = new List<object>();
data.Add(new { text= "JavaScript", pic = "javascript", description = "It is a lightweight interpreted or JIT-compiled programming language." });
data.Add(new { text= "TypeScript", pic = "typescript", description = "It is a typed superset of JavaScript that compiles to plain JavaScript." });
data.Add(new { text= "Angular", pic = "angular", description = "It is a TypeScript-based open-source web application framework." });
data.Add(new { text= "React", pic = "react", description = "A JavaScript library for building user interfaces. It can also render on the server using Node." });
data.Add(new { text= "Vue", pic = "vue", description = "A progressive framework for building user interfaces. it is incrementally adoptable." });
ViewData["data"] = data;
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>
The ListBoxis a graphical user interface component used to display a list of items. This sample illustrates how to integrate the item template to customize the list item's content and this can be specified by using ItemTemplate property.
In this sample, data is bound to the ListBox using the DataSource property.