ASP.NET MVC - Creating a SelectListItem with the disabled="disabled" attribute_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > ASP.NET MVC - Creating a SelectListItem with the disabled="disabled" attribute

ASP.NET MVC - Creating a SelectListItem with the disabled="disabled" attribute

 2011/10/25 8:11:19  fhuan123  http://fhuan123.iteye.com  我要评论(0)
  • 摘要:要完成这样的效果<optiondisabled="disabled">don'tclickthis</option>publicclassCustomSelectItem:SelectListItem{publicboolEnabled{get;set;}}publicstaticclassCustomHtmlHelpers{publicstaticMvcHtmlStringMyDropDownList(thisHtmlHelperhtml,IEnumerable<
  • 标签:.net ASP.NET MVC list item net

要完成这样的效果

<option disabled="disabled">don't click this</option>
?
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());
    }
}
?
发表评论
用户名: 匿名