创建与残疾人一个SelectListItem ="禁用"属性 [英] Creating a SelectListItem with the disabled="disabled" attribute

查看:204
本文介绍了创建与残疾人一个SelectListItem ="禁用"属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有看到一种方式来创建,通过的HtmlHelper ,一个 SelectListItem ,将吐出下面的HTML:

 <选项禁用=禁用>无效点击该< /选项>

的属性只有<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.mvc.selectlistitem.aspx\"><$c$c>SelectListItem有是:

 新SelectListItem {
  NAME =不要点击这个
  值=的String.Empty,
  选择= FALSE
}

我看到的唯一的选择就是


  1. 子类中的 SelectListItem 添加一个已启用属性来获取价值的看法

  2. 不使用HTML帮手的DropDownList

  3. 创建一个接受一个新的的HtmlHelper 扩展我的新 EnablableSelectList ,并增加了我的禁用属性。


解决方案

这是一些完全重新创建助手之前,我可能会尝试。其基本思想是,你从助手得到的HTML应该得到很好的形成,所以它应该是安全的解析。所以,你可以通过你自己的扩展,使用现有的延伸,但将功能添加禁用物品建立在这一想法。

像这样的东西可能会做(完全未经测试)

 公共类CustomSelectItem:SelectListItem
{
    公共BOOL启用{搞定;组; }
}公共静态类CustomHtmlHelpers
{
    公共静态MvcHtmlString MyDropDownList(这个HTML的HtmlHelper,IEnumerable的&LT; CustomSelectItem&GT;选择列表)
    {
        变种selectDoc = XDocument.Parse(html.DropDownList(,(IEnumerable的&所述; SelectListItem&GT)选择列表)的ToString());        从的XElement EL在selectDoc.Element(选择)VAR选项=后裔()
                                    选择El;        的foreach(期权VAR项)
        {
            VAR itemValue = item.Attribute(值);
            如果(selectList.Where(X =&GT;!x.Value == itemValue.Value)。单()启用)
                item.SetAttributeValue(禁用,已禁用);
        }        //重建的控制,重新与你修改的那些选项
        selectDoc.Root.ReplaceNodes(options.ToArray());
        返回MvcHtmlString.Create(selectDoc.ToString());
    }
}

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 =&QUOT;禁用&QUOT;属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆