asp.net-mvc – 如何在DropDownListFor的扩展中添加额外的html属
发布时间:2020-12-15 19:24:55 所属栏目:asp.Net 来源:网络整理
导读:我正在为DropDownListFor写一个扩展名: public static MvcHtmlString DropDownListForTModel,TProperty(this HtmlHelperTModel htmlHelper,ExpressionFuncTModel,TProperty expression,IEnumerableSelectListItem selectList,object htmlAttributes,bool en
我正在为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); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 如何在ASP.NET gridview的标题中放置一个按钮?
- asp.net-mvc – ASP.NET MVC依赖注入Unity与WCF服务 – 工作
- 如何在ASP.net MVC中正确执行异步方法?
- 在ASP.NET MVC视图页面中放置jQuery代码的位置?
- asp.net-mvc-4 – 如何获取没有隐藏输入的AntiForgeryToken
- 使用DropDownList的ASP.Net自定义验证器控件
- asp.net-mvc – MVC 3:使用EditorForModel隐藏ID属性
- .net – 简单注入器“由于在MVC验证期间已经处理了DbContex
- 如何为ASP.NET_SessionId cookie设置HttpOnly?
- asp.net – 当请求具有查询字符串时,Web服务的缓存不起作用
推荐文章
站长推荐
热点阅读