我正在为DropDownListFor写一个扩展名:
public static MvcHtmlString DropDownListFor<TModel,TProperty>(this HtmlHelper<TModel> htmlHelper,Expression<Func<TModel,TProperty>> expression,IEnumerable<SelectListItem> selectList,object htmlAttributes,bool enabled) { return htmlHelper.DropDownListFor(expression,selectList,null /* optionLabel */,HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)); }
我想要实现的是如果启用是false不会更改,但如果启用是真的我要添加@ disabled =“禁用”到html属性,然后再提供给AnonymousObjectToHtmlAttributes.
有什么想法呢?
解决方法
简单! HtmlHelper.AnonymousObjectToHtmlAttributes返回RouteValueDictionary.您可以向该字典添加值,您不需要向匿名对象添加属性.
public static MvcHtmlString DropDownListFor<TModel,bool enabled) { var attrs = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes); if (!enabled) { attrs.Add("disabled","disabled"); } return htmlHelper.DropDownListFor(expression,attrs); }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。