[英]Adding a class to HTML.DropDownListFor
我正在尝试向以下代码行添加类分配,
@Html.DropDownListFor(model => model.Business, new SelectList(
new List<Object>{
new { ... },
new { ... },
new { ... }
},
"value",
"text",
2))
在此站点上给出的一个答案( HTML.DropDownListFor类分配 )建议将其添加为一种重载方法,该方法将“对象htmlAttributes”作为最后一个参数。 但是,我看不到此参数是一个选项,所以我有点困惑这是如何实现的。
在我的示例中,我正在使用所有可能的重载方法,而htmlattribute没有任何方法。
DropDownListFor重载中的四个将HTML属性作为其最后一个参数。 这可能是最简单的。 修改您的示例,它将变为:
@Html.DropDownListFor(model => model.Business,
new SelectList(
new List<Object>{
new { ... },
new { ... },
new { ... }
},
"value",
"text",
2),
new {
@class = "myclass"
})
使用以下重载:
public static MvcHtmlString DropDownListFor<TModel, TProperty>(
this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression,
IEnumerable<SelectListItem> selectList,
IDictionary<string, Object> htmlAttributes
)
这样做:
@Html.DropDownListFor(model => model.Business,
new SelectList(new List<Object>
{
new { ... },
new { ... },
new { ... }
},
"value", "text", 2),
new { @class ="YourClass"})
或以下重载也将起作用:
public static MvcHtmlString DropDownListFor<TModel, TProperty>(
this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression,
IEnumerable<SelectListItem> selectList,
string optionLabel,
IDictionary<string, Object> htmlAttributes
)
这条路:
@Html.DropDownListFor(model => model.Business,
new SelectList(new List<Object>
{
new { ... },
new { ... },
}, "value", "text", 2),
null,new { @class ="YourClass"})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.