Example of Selection Limit in ASP.NET MVC MultiSelect Control
This sample demonstrates the maximum selection limit functionalities with checkbox of the MultiSelect. MultiSelect value can set restrictions based on the maximum selection length that can be selected.
@Html.UiMultiSelect("checkList", new UiMultiSelectOptions
{
DataSource = (IEnumerable<DropdownsDemoController.CountriesData>)ViewData["data"],
Mode = UiMultiSelectMode.CheckBox,
Placeholder = "Select countries",
MaximumSelectionLength = 3,
ShowDropDownIcon = true,
PopupHeight = "350px",
Fields = new UiDropDownFields { Value = "Code", Text = "Name" }
})
@Html.UiNumericTextBox("maxsel", new UiNumericTextBoxOptions { Format = "n0", Max = 19, Value = 3, Min = 1 })
@Html.UiButton(new UiButtonOptions { Id = "buttonApply", Text = "Apply", Variant = UiButtonVariant.Outline })
<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>CheckBox</h4>
@Html.UiMultiSelect("checkList", new UiMultiSelectOptions
{
DataSource = (IEnumerable<DropdownsDemoController.CountriesData>)ViewData["data"],
Mode = UiMultiSelectMode.CheckBox,
Placeholder = "Select countries",
MaximumSelectionLength = 3,
ShowDropDownIcon = true,
PopupHeight = "350px",
Fields = new UiDropDownFields { Value = "Code", Text = "Name" }
})
</div>
</div>
<div class="demo-property-panel" title="Properties">
<div class="demo-property-row">
<label class="example-label">Maximum selection length</label>
@Html.UiNumericTextBox("maxsel", new UiNumericTextBoxOptions { Format = "n0", Max = 19, Value = 3, Min = 1 })
</div>
<div class="demo-property-row">
@Html.UiButton(new UiButtonOptions { Id = "buttonApply", Text = "Apply", Variant = UiButtonVariant.Outline })
</div>
</div>
<script>
// Syncfusion: Apply clears the chosen items and sets maximumSelectionLength to the number.
document.getElementById('buttonApply').addEventListener('click', function () {
var checkList = window.Lumen.get('checkList');
checkList.clear();
checkList.setMaximumSelectionLength(window.Lumen.get('maxsel').getValue());
});
</script>
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/SelectionLimit.cshtml; it draws what ViewData holds.
public ActionResult SelectionLimit()
{
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 has built-in support to limit the value selected in Multiselect component. When the maximumSelectionLength property is set as 3, maximum of only 3 value will be selected in the MultiSelect.
The selection limit sample illustrates using the countries data.