Example of Icons and Images in ASP.NET MVC TreeView Control
This ASP.NET MVC TreeView example demonstrates the node can be configured by icons/images in TreeView. Click on icon or double click on node to expand/collapse it, and show the icons/images that configured with nodes.
- Music
- Gouttes.mp3
- Videos
- Naturals.mp4
- Wild.mpeg
- Documents
- Environment Pollution.docx
- Global Water, Sanitation, & Hygiene.docx
- Global Warming.ppt
- Social Network.pdf
- Youth Empowerment.pdf
- Pictures
- Camera Roll
- Wind.jpg
- Stone.jpg
- Downloads
- UI-Guide.pdf
- Tutorials.zip
- Game.exe
- TypeScript.7z
@Html.UiTreeView(new UiTreeViewOptions { Id = "tree", Nodes = (List<UiTreeNode>)ViewData["iconFields"] })
<style>
.control_wrapper { max-width: 500px; margin: auto; border: 1px solid #dddddd; border-radius: 3px; }
</style>
<div class="control-section">
<div class="control_wrapper">
@Html.UiTreeView(new UiTreeViewOptions { Id = "tree", Nodes = (List<UiTreeNode>)ViewData["iconFields"] })
</div>
</div>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class TreeViewController : Controller
{
// The page is Views/TreeView/IconsandImages.cshtml; it draws what ViewData holds.
public ActionResult IconsandImages()
{
ViewData["iconFields"] = new List<UiTreeNode>
{
new UiTreeNode { Id = "01", Text = "Music", IconCss = "e-icons e-folder", Children =
{
new UiTreeNode { Id = "01-01", Text = "Gouttes.mp3", IconCss = "e-icons e-audio" }
} },
new UiTreeNode { Id = "02", Text = "Videos", IconCss = "e-icons e-folder", Children =
{
new UiTreeNode { Id = "02-01", Text = "Naturals.mp4", IconCss = "e-icons e-video" },
new UiTreeNode { Id = "02-02", Text = "Wild.mpeg", IconCss = "e-icons e-video" }
} },
new UiTreeNode { Id = "03", Text = "Documents", Expanded = true, IconCss = "e-icons e-folder", Children =
{
new UiTreeNode { Id = "03-01", Text = "Environment Pollution.docx", IconCss = "e-icons e-file" },
new UiTreeNode { Id = "03-02", Text = "Global Water, Sanitation, & Hygiene.docx", IconCss = "e-icons e-file" },
new UiTreeNode { Id = "03-03", Text = "Global Warming.ppt", IconCss = "e-icons e-file" },
new UiTreeNode { Id = "03-04", Text = "Social Network.pdf", IconCss = "e-icons e-file" },
new UiTreeNode { Id = "03-05", Text = "Youth Empowerment.pdf", IconCss = "e-icons e-file" }
} },
new UiTreeNode { Id = "04", Text = "Pictures", IconCss = "e-icons e-folder", Children =
{
new UiTreeNode { Id = "04-01", Text = "Camera Roll", IconCss = "e-icons e-folder" },
new UiTreeNode { Id = "04-02", Text = "Wind.jpg", IconCss = "e-icons e-image" },
new UiTreeNode { Id = "04-03", Text = "Stone.jpg", IconCss = "e-icons e-image" }
} },
new UiTreeNode { Id = "05", Text = "Downloads", IconCss = "e-icons e-folder", Children =
{
new UiTreeNode { Id = "05-01", Text = "UI-Guide.pdf", IconCss = "e-icons e-file" },
new UiTreeNode { Id = "05-02", Text = "Tutorials.zip", IconCss = "e-icons e-zip" },
new UiTreeNode { Id = "05-03", Text = "Game.exe", IconCss = "e-icons e-exe" },
new UiTreeNode { Id = "05-04", Text = "TypeScript.7z", IconCss = "e-icons e-zip" }
} }
};
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 TreeView component has the built-in option to customize each node's appearance with the icons and images by mapping the iconCss and imageUrl fields.
In this demo, the TreeView is showcased like a file system with custom icons and images.