Example of Custom Values in ASP.NET MVC MultiSelect Control
This sample demonstrates the custom value functionalities of the MultiSelect. Type a character(s) in the MultiSelect element that are not present in the dataSource. You can select and tag that custom typed characters as new item from the suggestion list.
@Html.UiMultiSelect("default", new UiMultiSelectOptions
{
Mode = UiMultiSelectMode.Box,
Placeholder = "Select countries",
AllowCustomValue = true,
DataSource = (IEnumerable<DropdownsDemoController.CountriesData>)ViewData["data"],
Fields = new UiDropDownFields { Text = "Name", Value = "Code" },
Value = (string[])ViewData["val"]
})
<style>
.control-section .content { margin: 0 auto; width: 250px; }
.control-section h4 { margin: 0 0 8px; }
</style>
@using Lumen.UI.Mvc.Demo.Controllers
<div class="control-section">
<div class="content" style="padding-top:15px">
<h4>Custom Value Support</h4>
@Html.UiMultiSelect("default", new UiMultiSelectOptions
{
Mode = UiMultiSelectMode.Box,
Placeholder = "Select countries",
AllowCustomValue = true,
DataSource = (IEnumerable<DropdownsDemoController.CountriesData>)ViewData["data"],
Fields = new UiDropDownFields { Text = "Name", Value = "Code" },
Value = (string[])ViewData["val"]
})
</div>
</div>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class MultiSelectController : Controller
{
// The page is Views/MultiSelect/CustomValue.cshtml; it draws what ViewData holds.
public ActionResult CustomValue()
{
ViewData["data"] = new CountriesData().CountriesList();
return View();
}
public class CountriesData
{
public string Name { get; set; }
public string Code { get; set; }
public List<CountriesData> CountriesList()
{
List<CountriesData> country = new List<CountriesData>();
country.Add(new CountriesData { Name = "Australia", Code = "AU" });
country.Add(new CountriesData { Name = "Bermuda", Code = "BM" });
country.Add(new CountriesData { Name = "Canada", Code = "CA" });
// ... 16 more rows
return country;
}
}
}
}
@* 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 MultiSelect allows the user to add a non-present option to the component value when the allowCustomValue is enabled. While selecting new custom value the customValueSelection event will be triggered.