Example of API in ASP.NET MVC ListBox Control
This sample demonstrates the API functionalities of the ListBox component by using its properties from the property pane. Select any combination of properties from the property pane to achieve desired functionalities in ListBox.
- Cabbage
- Chickpea
- Garlic
- Green bean
- Horse gram
- Nopal
- Onion
- Pumpkins
- Spinach
- Wheat grass
- Yarrow
Sort order
Selection Mode
@using Lumen.UI.Mvc.Demo.Controllers
@Html.UiListBox("listbox", new UiListBoxOptions
{
DataSource = (IEnumerable<object>)ViewData["vegetableData"],
Fields = new UiDropDownFields { GroupBy = "Category", Text = "Vegetable", Value = "Id" }
})
@Html.UiDropDownList("ddl", new UiDropDownListOptions
{
PopupHeight = "200px",
Value = "None",
DataSource = (IEnumerable<object>)ViewData["sortOrder"],
Fields = new UiDropDownFields { Text = "Text" },
OnChange = "orderChange"
})
@Html.UiDropDownList("mode", new UiDropDownListOptions
{
PopupHeight = "200px",
Value = "Multiple",
DataSource = (IEnumerable<object>)ViewData["selectionType"],
Fields = new UiDropDownFields { Text = "Text" },
OnChange = "modeChange"
})
@using Lumen.UI.Mvc.Demo.Controllers
<div class="control-section">
<div id="listbox-control1" style="margin: 0 auto; width: 300px; padding-top: 30px;">
@Html.UiListBox("listbox", new UiListBoxOptions
{
DataSource = (IEnumerable<object>)ViewData["vegetableData"],
Fields = new UiDropDownFields { GroupBy = "Category", Text = "Vegetable", Value = "Id" }
})
</div>
</div>
<div class="demo-property-panel" title="Properties">
<div class="demo-property-row">
<div>Sort order</div>
@Html.UiDropDownList("ddl", new UiDropDownListOptions
{
PopupHeight = "200px",
Value = "None",
DataSource = (IEnumerable<object>)ViewData["sortOrder"],
Fields = new UiDropDownFields { Text = "Text" },
OnChange = "orderChange"
})
</div>
<div class="demo-property-row">
<div>Selection Mode</div>
@Html.UiDropDownList("mode", new UiDropDownListOptions
{
PopupHeight = "200px",
Value = "Multiple",
DataSource = (IEnumerable<object>)ViewData["selectionType"],
Fields = new UiDropDownFields { Text = "Text" },
OnChange = "modeChange"
})
</div>
</div>
<script>
// Syncfusion: the two lists set the sortOrder and the selection mode of the ListBox.
function orderChange(value) { if (value) { window.Lumen.get('listbox').setSortOrder(value); } }
function modeChange(value) { if (value) { window.Lumen.get('listbox').setSelectionMode(value); } }
</script>
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/Api.cshtml; it draws what ViewData holds.
public ActionResult Api()
{
ViewData["vegetableData"] = new Vegetables().VegetablesList();
List<object> sortOrder = new List<object>();
sortOrder.Add(new { Text = "None"});
sortOrder.Add(new { Text = "Ascending"});
sortOrder.Add(new { Text = "Descending" });
ViewData["sortOrder"] = sortOrder;
List<object> selectionType = new List<object>();
selectionType.Add(new { Text = "Single" });
selectionType.Add(new { Text = "Multiple" });
ViewData["selectionType"] = selectionType;
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;
}
}
}
}
@* 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>
In this demo, a ListBox is rendered with grouping feature by setting the GroupBy property in Fields property. This sample has been showcased with following set of properties,
- You can switch to
SingleorMultipleselection mode by selecting the mode from the selection mode dropdown list. - You can switch to
None,Ascending, orDescendingsort orders by selecting the sort order from the sort order dropdown list.
In this sample, by default, grouping is enabled and vegetableData is grouped based on its category. The user can change the sort order and selection type using dropdownlist.