Example of Disabled Items in ASP.NET MVC DropDownList Control
This example showcases the disabled items of DropDownList. When you click on the DropDownList the popup will open, and you will notice that the disabled items are greyed out and cannot be selected.
Vegetable
@Html.UiDropDownList("status", new UiDropDownListOptions
{
Placeholder = "Select Status",
AllowFiltering = true,
DataSource = (IEnumerable<DropdownsDemoController.Status>)ViewData["statusdata"],
Fields = new UiDropDownFields { Value = "ID", Text = "Text", Disabled = "State" }
})
@Html.UiDropDownList("vegetable", new UiDropDownListOptions
{
Placeholder = "Select Vegetable",
DataSource = (IEnumerable<DropdownsDemoController.VegetablesData>)ViewData["vegetabledata"],
Fields = new UiDropDownFields { GroupBy = "Category", Text = "Vegetable", Value = "ID", Disabled = "Status" }
})
<style>
.control-section .content { margin: 0 auto; width: 250px; }
</style>
@using Lumen.UI.Mvc.Demo.Controllers
<div class="control-section">
<div id="statusList" style="padding-top:15px">
<div class="content">
<h3> Status </h3>
@Html.UiDropDownList("status", new UiDropDownListOptions
{
Placeholder = "Select Status",
AllowFiltering = true,
DataSource = (IEnumerable<DropdownsDemoController.Status>)ViewData["statusdata"],
Fields = new UiDropDownFields { Value = "ID", Text = "Text", Disabled = "State" }
})
</div>
</div>
<div id="vegetableList" style="padding-top:15px">
<div class="content">
<h3> Vegetable </h3>
@Html.UiDropDownList("vegetable", new UiDropDownListOptions
{
Placeholder = "Select Vegetable",
DataSource = (IEnumerable<DropdownsDemoController.VegetablesData>)ViewData["vegetabledata"],
Fields = new UiDropDownFields { GroupBy = "Category", Text = "Vegetable", Value = "ID", Disabled = "Status" }
})
</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 DropDownListController : Controller
{
// The page is Views/DropDownList/DisabledItems.cshtml; it draws what ViewData holds.
public ActionResult DisabledItems()
{
ViewData["statusdata"] = new Status().StatusDataList();
ViewData["vegetabledata"] = new VegetablesData().VegetablesDataList();
return View();
}
public class Status
{
public string ID { get; set; }
public string Text { get; set; }
public bool State { get; set; }
public List<Status> StatusDataList()
{
List<Status> status = new List<Status>();
status.Add(new Status() { ID = "Item1", Text = "Open", State = false });
status.Add(new Status() { ID = "Item2", Text = "Waiting for Customer", State = false });
status.Add(new Status() { ID = "Item3", Text = "On Hold", State = true });
status.Add(new Status() { ID = "Item4", Text = "Follow-up", State = false });
status.Add(new Status() { ID = "Item5", Text = "Closed", State = true });
status.Add(new Status() { ID = "Item6", Text = "Solved", State = false });
status.Add(new Status() { ID = "Item7", Text = "Feature Request", State = false });
return status;
}
}
public class VegetablesData
{
public string Vegetable { get; set; }
public string Category { get; set; }
public string ID { get; set; }
public bool Status { get; set; }
public List<VegetablesData> VegetablesDataList()
{
List<VegetablesData> veg = new List<VegetablesData>();
veg.Add(new VegetablesData { Vegetable = "Cabbage", Category = "Leafy and Salad", ID = "item1", Status = true });
veg.Add(new VegetablesData { Vegetable = "Pumpkins", Category = "Leafy and Salad", ID = "item2", Status = false });
veg.Add(new VegetablesData { Vegetable = "Spinach", Category = "Leafy and Salad", ID = "item3", Status = true });
veg.Add(new VegetablesData { Vegetable = "Wheat grass", Category = "Leafy and Salad", ID = "item4", Status = false });
veg.Add(new VegetablesData { Vegetable = "Yarrow", Category = "Leafy and Salad", ID = "item5", Status = false });
veg.Add(new VegetablesData { Vegetable = "Chickpea", Category = "Beans", ID = "item6", Status = true });
veg.Add(new VegetablesData { Vegetable = "Green bean", Category = "Beans", ID = "item7", Status = false });
veg.Add(new VegetablesData { Vegetable = "Horse gram", Category = "Beans", ID = "item8", Status = true });
veg.Add(new VegetablesData { Vegetable = "Garlic", Category = "Bulb and Stem", ID = "item9", Status = false });
veg.Add(new VegetablesData { Vegetable = "Nopal", Category = "Bulb and Stem", ID = "item10", Status = true });
veg.Add(new VegetablesData { Vegetable = "Onion", Category = "Bulb and Stem", ID = "item11", Status = false });
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>
The DropDownList provides options for individual items to be in either an enabled or disabled state for specific scenarios. Once an item is disabled, it cannot be selected as a value for the component. To configure the disabled item columns, use the fields.disabled property.