代码之家  ›  专栏  ›  技术社区  ›  Mark

使用disabled=“disabled”属性创建SelectListItem

  •  25
  • Mark  · 技术社区  · 16 年前

    HtmlHelper ,一个 SelectListItem 将生成以下HTML:

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

    SelectListItem 有:

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

    我唯一的选择就是

    1. 添加 Enabled 属性获取视图的值
    2. 不使用 HTML helper for DropDownList
    3. 创建新的 HtmlHelper公司 接受我的新 EnablableSelectList 加上我的 disabled 属性。
    5 回复  |  直到 16 年前
        1
  •  20
  •   David Murdoch    14 年前

    在完全重新创建助手之前,我可能会尝试这样做。其基本思想是,您从helper获得的Html应该是格式良好的,因此解析应该是安全的。因此,您可以在这个想法的基础上创建自己的扩展,该扩展使用现有的扩展,但添加了禁用项的功能。

    像这样的事情也许可以(完全未经测试)

    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());
        }
    }
    
        2
  •  27
  •   Mihkel Müür    11 年前

    这个 Disabled 属性从ASP.NET MVC 5.2开始受支持:

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

    看到了吗 API reference

        3
  •  5
  •   staccata    12 年前

    Clientside选项:例如,如果您给dropdownlist一个类“custom”,而不可选择的项的值为-1(例如),那么您可以执行如下操作:

    $('select.custom option[value=-1]').each(function () {
        $(this).attr("disabled", "disabled");
    });
    
        4
  •  0
  •   Thyamine    16 年前

    如果您所要做的只是阻止用户从列表中选择某个值,那么使用输入验证似乎是更简单、更省时的方法。如果你想确认他们已经做出了一个选择的话,你很有可能会这样做。

        5
  •  0
  •   Adel Mourad    7 年前

    控制器:

    var ExpectedShipmentsRange=新列表();

    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;
    

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

    控制器:

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

    查看:

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

    -----选项3不支持禁用选项:

    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");
    

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