Example of CustomValue in ASP.NET MVC ComboBox Control
This sample demonstrates the custom value functionalities of the ComboBox. When the typed character(s) is not present in the list, a button will be shown in the popup list. By clicking on this button, the custom value character is added in the existing list as a new item.
No matched item, do you want to add it as new item in list?
@Html.UiComboBox("country", new UiComboBoxOptions
{
Placeholder = "Select a country",
AllowFiltering = true,
PopupHeight = "300px",
Width = "300px",
DataSource = (IEnumerable<DropdownsDemoController.CountriesData>)ViewData["data"],
NoRecordsTemplate = "#noRecords",
Fields = new UiDropDownFields { Text = "Name", Value = "Code" }
})
<style>
.control-wrapper { margin: 0 auto; width: 250px; }
#nodata { height: 55px; opacity: .7; }
</style>
@using Lumen.UI.Mvc.Demo.Controllers
<template id="noRecords"><div><div id="nodata"> No matched item, do you want to add it as new item in list?</div><button id="btn" type="button" class="ui-btn">Add New Item</button></div></template>
<div class="control-section">
<div class="control-wrapper">
<div style="padding-top:75px;">
@Html.UiComboBox("country", new UiComboBoxOptions
{
Placeholder = "Select a country",
AllowFiltering = true,
PopupHeight = "300px",
Width = "300px",
DataSource = (IEnumerable<DropdownsDemoController.CountriesData>)ViewData["data"],
NoRecordsTemplate = "#noRecords",
Fields = new UiDropDownFields { Text = "Name", Value = "Code" }
})
</div>
</div>
</div>
<script>
// Syncfusion: the button of the popup adds the typed text to the list and selects it.
document.addEventListener('click', function (e) {
if (!e.target.closest || !e.target.closest('#btn')) { return; }
var country = window.Lumen.get('country');
var customValue = country.getText();
country.hidePopup();
country.addItem({ value: customValue, text: customValue });
country.setValue(customValue);
});
</script>
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/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 ComboBox allows the user to give input as custom value which is not required to present in the predefined set of values. By default, this support is enabled by allowCustom property. In this case, both text field and value field are considered as same. The custom value will be sent to post back handler when a form is about to be submitted.