我正在尝试为以下内容编写扩展名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));
}
Run Code Online (Sandbox Code Playgroud)
我想要实现的是如果启用是假的没有改变但是如果启用是真我想@disabled="disabled"在给它们之前添加到html属性AnonymousObjectToHtmlAttributes.
关于如何做到这一点的任何想法?
arc*_*hil 33
简单!HtmlHelper.AnonymousObjectToHtmlAttributes回报RouteValueDictionary.您可以为该字典添加值,您不需要向匿名对象添加属性.
public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, object htmlAttributes, bool enabled)
{
var attrs = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
if (!enabled)
{
attrs.Add("disabled", "disabled");
}
return htmlHelper.DropDownListFor(expression, selectList, null /* optionLabel */, attrs);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17948 次 |
| 最近记录: |