Creating a SelectListItem with the disabled="disabled" attribute

asp select list item
selectlistitem contains
selectlistitem in web api
selectlist vs selectlistitem
dropdownlistfor disabled option
mvc dropdownlist disable
the type or namespace name 'selectlistitem' could not be found
dropdown with disabled items

I'm not seeing a way to create, via the HtmlHelper, a SelectListItem that will spit out the following HTML:

<option disabled="disabled">don't click this</option>

The only properties SelectListItem has are:

new SelectListItem{
  Name = "don't click this",
  Value = string.Empty,
  Selected = false
}

The only option I see is to

  1. Subclass the SelectListItem to add an Enabled property to get the value to the view
  2. Not use the HTML helper for DropDownList
  3. Create a new HtmlHelper extension that accepts my new EnablableSelectList and adds my disabled attribute.

This is something I might try before recreating the helper completely. The basic idea is that the Html you get from the helper should be well formed, so it should be safe to parse. So you can build on that idea by making your own extension that uses the existing extension but adds the functionality to disable the items.

Something like this might do (totally untested)

public class CustomSelectItem : SelectListItem
{
    public bool Enabled { get; set; }
}

public static class CustomHtmlHelpers
{
    public static MvcHtmlString MyDropDownList(this HtmlHelper html, IEnumerable<CustomSelectItem> selectList)
    {
        var selectDoc = XDocument.Parse(html.DropDownList("", (IEnumerable<SelectListItem>)selectList).ToString());

        var options = from XElement el in selectDoc.Element("select").Descendants()
                                    select el;

        foreach (var item in options)
        {
            var itemValue = item.Attribute("value");
            if (!selectList.Where(x => x.Value == itemValue.Value).Single().Enabled)
                item.SetAttributeValue("disabled", "disabled");
        }

        // rebuild the control, resetting the options with the ones you modified
        selectDoc.Root.ReplaceNodes(options.ToArray());
        return MvcHtmlString.Create(selectDoc.ToString());
    }
}

SelectListItem.Disabled Property (Microsoft.AspNetCore.Mvc , is disabled. This property is typically rendered as a disabled="disabled" attribute in the HTML <option> element. Microsoft Build May 19-20, 2020 | Digital event Gets or sets a value that indicates whether this SelectListItem is disabled. Creating a SelectListItem with the disabled=“disabled” attribute 141 The ViewData item that has the key 'MY KEY' is of type 'System.String' but must be of type 'IEnumerable<SelectListItem>'

The Disabled property is supported since ASP.NET MVC 5.2:

new SelectListItem {
    // ...
    Disabled = true
}

See the API reference.

SelectListItem.Disabled Property (System.Web.Mvc), Microsoft Build May 19-20, 2020 | Digital event. We look forward to bringing Gets or sets a value that indicates whether this SelectListItem is disabled. C# Copy. Gets or sets a value that indicates the display text of this SelectListItem. This property is typically rendered as the inner HTML in the HTML <option> element. Value: Gets or sets a value that indicates the value of this SelectListItem. This property is typically rendered as a value="" attribute in the HTML <option> element.

Clientside option: if you for example give your dropdownlist a class 'custom' and the items that should be unselectable the value -1 (for example), then you can do something like:

$('select.custom option[value=-1]').each(function () {
    $(this).attr("disabled", "disabled");
});

Creating a SelectListItem with the disabled=“disabled” attribute, I'm not seeing a way to create, via the HtmlHelper, a SelectListItem that will spit out the following HTML: <option disabled="disabled">don't click this</option>  Disabled: Gets or sets a value that indicates whether this SelectListItem is disabled. Group: Represents the optgroup HTML element this item is wrapped into. In a select list, multiple groups with the same name are supported. They are compared with reference equality. Selected: Gets or sets a value that indicates whether this SelectListItem is selected. Text

If all you are trying to do is prevent a user from selecting a certain value from the list, it seems like the simpler and more time-efficient way to do it is to use input validation. Which you may quite possibly be doing anyways, if you want to verify they've made a selection to begin with.

Why does SelectList no longer support a constructor which takes a , In Asp.Net MVC the SelectList has a constructor overload of the form: I'm creating a drop down list that is grouped and also shows disabled  using System.Collections.Generic; using System.Web.Routing; namespace System.Web.Mvc.Html { public static class HtmlHelperExtensions { public static MvcHtmlString DropDownList(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItemExtends> selectList, object htmlAttributes)//, Func<object, bool> ItemDisabled) { //Creating a select element using TagBuilder class which will create a dropdown.

-----Option 1 Controller:

var ExpectedShipmentsRange = new List();

ExpectedShipmentsRange.Add(new SelectListItem() { Text = "Selected number of shipments", Value="0", Disabled = true, Selected  = true });
ExpectedShipmentsRange.Add(new SelectListItem() { Text = "0 to 20 shipments", Value = "0-20" });
ExpectedShipmentsRange.Add(new SelectListItem() { Text = "20 to 40 shipments", Value = "20-40" });

ViewBag.ExpectedShipmentsRange = ExpectedShipmentsRange;

View:

@Html.DropDownListFor(m => m.ExpectedShipments, (IEnumerable<SelectListItem>)@ViewBag.ExpectedShipmentsRange, new { @class = "form-control" })

-----Option 2 Controller:

ViewBag.citiesSa = _dbContext.Countries.ToList();

View:

@Html.DropDownListFor(m => m.City, new SelectList(@ViewBag.citiesSa, "Id", "Name"), "Select your city", new { @class = "form-control" })

-----Option 3 does not support disabled option:

List<SelectListItem> ExpectedShipmentsRange = new List<SelectListItem>();
ExpectedShipmentsRange.Add(new SelectListItem() { Text = "0 to 20 shipments", Value = "0-20" });
ExpectedShipmentsRange.Add(new SelectListItem() { Text = "20 to 40 shipments", Value = "20-40" });

ViewBag.ExpectedShipmentsRange = new SelectList(ExpectedShipmentsRange, "Value", "Text");

View:

@Html.DropDownListFor(m => m.ExpectedShipments, (SelectList)@ViewBag.ExpectedShipmentsRange, new { @class = "form-control" })

Display inactive element in a dropdownlist as disabled and still view it., Select( x => new SelectListItem { Selected = false, Value = x.Id.ToString(​CultureInfo. I just want to make the item visible but disable it. Reply. I just want to create a disabled dropdownlist. I have already created SelectList name "statusList". Through which I am trying to call the status. but I want to make it disbled and make one status as by default

ASP.NET MVC, To accomplish this effect <option disabled="disabled">don't click this</option>. public class CustomSelectItem : SelectListItem { public bool Enabled { get; set; }  Creating a SelectListItem with the disabled=“disabled” attribute. I'm not seeing a way to create, via the HtmlHelper, a SelectListItem that will spit out the following HTML: The only properties SelectListItem has are: Create a new HtmlHelper extension that accepts my new EnablableSelectList and adds my disabled attribute.

Creating a SelectListItem with the disabled="disabled , I'm not seeing a way to create, via the HtmlHelper , a SelectListItem that will spit out the following HTML: <option disabled="disabled">don't click this</option>. C# (CSharp) SelectList - 30 examples found. These are the top rated real world C# (CSharp) examples of SelectList extracted from open source projects. You can rate examples to help us improve the quality of examples.

HTML option disabled Attribute, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java  Gets or sets a value that indicates whether this <xref data-throw-if-not-resolved="true" uid="System.Web.Mvc.SelectListItem"></xref> is disabled.</p>

Comments
  • +1 That's definitely lighter than recreating the entire helper extension; however, if I was going to recreate it, I would probably just steal the source for the Helper Extension and tweak it a bit.
  • It is also the joke of the century! In short, it has no effect on the retarded helper called DropdownList! :)
  • @Mrchief you need to use IEnumerable<SelectListItem> for the disabled flag to be respected, and not SelectList. See: stackoverflow.com/questions/29424365/…
  • Thanks for the useful info @co7e!
  • I already have input validation that invalidates the selection, but all other browsers, besides IE, will not let you select the item at all if the disabled="disabled" attribute is present.
  • Going forward IE 8 will support it, or does support it. For IE 6 and 7 I would agree that you'd have to try and create your own Item, but I'm not certain the best approach there. Again, I would look at how long it would take to work on that, and see if it's worth the time spent troubleshooting it given what browsers/version users will be using.
  • Actually, IE7 does obey the disabled="disabled". I must retract all my disdain for IE....on this issue. I'm just really surprised the Enabled attribute was left off the SelectListItem.