Example of Grouping and Icon in ASP.NET MVC ComboBox Control
This sample demonstrates the grouping and icons supports of the ComboBox. Type a character in the ComboBox element or click on the drodown icon to choose an item from the categorized list/icons list.
Icons
@Html.UiComboBox("vegetables", new UiComboBoxOptions
{
Placeholder = "Select a vegetable",
PopupHeight = "200px",
DataSource = (IEnumerable<DropdownsDemoController.Vegetables>)ViewData["groupdata"],
Fields = new UiDropDownFields { GroupBy = "Category", Text = "Vegetable", Value = "Id" }
})
@Html.UiComboBox("icons", new UiComboBoxOptions
{
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.UiComboBox("vegetables", new UiComboBoxOptions
{
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.UiComboBox("icons", new UiComboBoxOptions
{
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 ComboBoxController : Controller
{
// The page is Views/ComboBox/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 ComboBox 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 ComboBox is populated with icons that is rendered by mapping the iconCss field.