asp.net-mvc – 如何在DropDownListFor的扩展中添加额外的html属性
发布时间:2020-05-23 05:00:08 所属栏目:asp.Net 来源:互联网
导读:我正在为DropDownListFor写一个扩展名: public static MvcHtmlString DropDownListForTModel, TProperty(this HtmlHelperTModel htmlHelper, ExpressionFuncTModel, TProperty expression, IEnumerableSelectListItem
|
我正在为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 – IE8 Win7 Facebook Connect问题
- asp.net-mvc – ASP.NET MVC在HTTP DELETE请求上
- asp.net-mvc – 如何从ASP.NET MVC中的JSONResul
- asp.net – 如何通过web.config文件中的Access-C
- asp.net-mvc-3 – ASP.NET MVC 3 – 在jquery对话
- ASP.NET API(MVC) 对APP接口(Json格式)接收数据与
- asp.net – BC30560:’ExtensionAttribute’在名
- asp.net – 跟踪间歇性“对象引用未设置为对象的
- asp.net – 使用纯CSS定义asp:GridView的全局网
- ASP.NET CompareValidator问题
热点阅读
