Example of Grouping and Icon in ASP.NET MVC DropDownList Control
This sample demonstrates the grouping and icons supports of the DropDownList. Click the DropDownList element and select an item from the categorized list/icons list.
Icons
@Html.UiDropDownList("vegetables", new UiDropDownListOptions
{
Placeholder = "Select a vegetable",
PopupHeight = "200px",
DataSource = (IEnumerable<DropdownsDemoController.Vegetables>)ViewData["groupdata"],
Fields = new UiDropDownFields { GroupBy = "Category", Text = "Vegetable", Value = "Id" }
})
@Html.UiDropDownList("icons", new UiDropDownListOptions
{
Placeholder = "Select a social media",
PopupHeight = "200px",
DataSource = (IEnumerable<DropdownsDemoController.SocialMedia>)ViewData["icondata"],
Fields = new UiDropDownFields { Text = "SocialMediaName", IconCss = "Class", Value = "Id" }
})
<style>
.control-section .content { margin: 0 auto; width: 250px; }
.socialicon { display: inline-block; width: 18px; text-align: center; font-weight: 700; font-size: 14px; }
.facebook::before { content: "f"; }
.google-plus::before { content: "g+"; }
.instagram::before { content: "\25A3"; }
.linkedin::before { content: "in"; }
.skype::before { content: "s"; }
.tumblr::before { content: "t"; }
.twitter::before { content: "t"; }
.vimeo::before { content: "v"; }
.whatsapp::before { content: "w"; }
.youtube::before { content: "\25B6"; }
</style>
@using Lumen.UI.Mvc.Demo.Controllers
<div class="control-section">
<div id="groupList" style="padding-top:15px">
<div class="content">
<h3>Grouping</h3>
@Html.UiDropDownList("vegetables", new UiDropDownListOptions
{
Placeholder = "Select a vegetable",
PopupHeight = "200px",
DataSource = (IEnumerable<DropdownsDemoController.Vegetables>)ViewData["groupdata"],
Fields = new UiDropDownFields { GroupBy = "Category", Text = "Vegetable", Value = "Id" }
})
</div>
</div>
<div id="iconList" style="padding-top:15px">
<div class="content">
<h3>Icons</h3>
@Html.UiDropDownList("icons", new UiDropDownListOptions
{
Placeholder = "Select a social media",
PopupHeight = "200px",
DataSource = (IEnumerable<DropdownsDemoController.SocialMedia>)ViewData["icondata"],
Fields = new UiDropDownFields { Text = "SocialMediaName", IconCss = "Class", Value = "Id" }
})
</div>
</div>
</div>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class DropDownListController : Controller
{
// The page is Views/DropDownList/GroupingAndIcon.cshtml; it draws what ViewData holds.
public ActionResult GroupingAndIcon()
{
ViewData["groupdata"] = new Vegetables().VegetablesList();
ViewData["icondata"] = new SocialMedia().SocialMediaList();
return View();
}
public class Vegetables
{
public string Vegetable { get; set; }
public string Category { get; set; }
public string Id { get; set; }
public List<Vegetables> VegetablesList()
{
List<Vegetables> veg = new List<Vegetables>();
veg.Add(new Vegetables { Vegetable = "Cabbage", Category= "Leafy and Salad", Id= "item1" });
veg.Add(new Vegetables { Vegetable = "Chickpea", Category= "Beans", Id= "item2" });
veg.Add(new Vegetables { Vegetable = "Garlic", Category= "Bulb and Stem", Id= "item3" });
veg.Add(new Vegetables { Vegetable = "Green bean", Category= "Beans", Id= "item4" });
veg.Add(new Vegetables { Vegetable = "Horse gram", Category= "Beans", Id= "item5" });
veg.Add(new Vegetables { Vegetable = "Nopal", Category= "Bulb and Stem", Id= "item6" });
veg.Add(new Vegetables { Vegetable = "Onion", Category= "Bulb and Stem", Id= "item7" });
veg.Add(new Vegetables { Vegetable = "Pumpkins", Category= "Leafy and Salad", Id= "item8" });
veg.Add(new Vegetables { Vegetable = "Spinach", Category= "Leafy and Salad", Id= "item9" });
veg.Add(new Vegetables { Vegetable = "Wheat grass", Category= "Leafy and Salad", Id= "item10" });
veg.Add(new Vegetables { Vegetable = "Yarrow", Category = "Leafy and Salad", Id = "item11" });
return veg;
}
}
public class SocialMedia
{
public string Class { get; set; }
public string SocialMediaName { get; set; }
public string Id { get; set; }
public List<SocialMedia> SocialMediaList()
{
List<SocialMedia> media = new List<SocialMedia>();
media.Add(new SocialMedia{ Class= "facebook", SocialMediaName= "Facebook", Id= "media1" });
media.Add(new SocialMedia{ Class= "google-plus", SocialMediaName= "Google Plus", Id= "media2" });
media.Add(new SocialMedia{ Class= "instagram", SocialMediaName= "Instagram", Id= "media3" });
media.Add(new SocialMedia{ Class= "linkedin", SocialMediaName= "LinkedIn", Id= "media4" });
media.Add(new SocialMedia{ Class= "skype", SocialMediaName= "Skype", Id= "media5" });
media.Add(new SocialMedia{ Class= "tumblr", SocialMediaName= "Tumblr", Id= "media6" });
media.Add(new SocialMedia{ Class= "twitter", SocialMediaName= "Twitter", Id= "media7" });
media.Add(new SocialMedia{ Class= "vimeo", SocialMediaName= "Vimeo", Id= "media8" });
media.Add(new SocialMedia{ Class= "whatsapp", SocialMediaName= "WhatsApp", Id= "media9" });
media.Add(new SocialMedia { Class = "youtube", SocialMediaName= "YouTube", Id = "media10" });
return media;
}
}
}
}
@* 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 DropDownList allows to group the relevant items under a corresponding category by mapping the groupBy field, and allows to load the list items with icons.
The grouping sample illustrates how the vegetables are grouped based on its category.
The 2nd DropDownList is populated with icons that is rendered by mapping the iconCss field.