DropDownListFor <TModel,TProperty> 方法接受另一个参数
DropDownListFor <TModel,TProperty> method to take another parameter
在这里我使用枚举绑定下拉列表。(绑定成功)。但我想添加css class,但在这里需要添加,
"DropDownListFor "<"TModel,TProperty">“为 css class 名称获取另一个参数的方法。”
我的下拉列表
@Html.DropDownListFor(m => m.ReportType)
但我需要以下一个在 MyDropdownlist 方法中接受,
@Html.DropDownListFor(m => m.ReportType, new {@class="form-control"})
助手Class(下拉列表方法)
public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression)
where TModel : class
{
TProperty value = htmlHelper.ViewData.Model == null
? default(TProperty)
: expression.Compile()(htmlHelper.ViewData.Model);
string selected = value == null ? String.Empty : value.ToString();
return htmlHelper.DropDownListFor(expression, createSelectList(expression.ReturnType, selected));
}
我从 This Link 那里得到了这个。在那篇文章的评论部分有人问了完全相同的问题..
我认为您需要将参数添加为 object htmlAttributes
并使用 htmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)
创建下拉列表。
您的完整帮助程序代码将变为:
public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression, object htmlAttributes )
where TModel : class
{
TProperty value = htmlHelper.ViewData.Model == null
? default(TProperty)
: expression.Compile()(htmlHelper.ViewData.Model);
string selected = value == null ? String.Empty : value.ToString();
return htmlHelper.DropDownListFor(expression, createSelectList(expression.ReturnType, selected), null, htmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
}
我发现在像您这样的情况下,最好的解决方案通常是查看 SelectExtensions
的实际源代码。
在这里我使用枚举绑定下拉列表。(绑定成功)。但我想添加css class,但在这里需要添加,
"DropDownListFor "<"TModel,TProperty">“为 css class 名称获取另一个参数的方法。”
我的下拉列表
@Html.DropDownListFor(m => m.ReportType)
但我需要以下一个在 MyDropdownlist 方法中接受,
@Html.DropDownListFor(m => m.ReportType, new {@class="form-control"})
助手Class(下拉列表方法)
public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression)
where TModel : class
{
TProperty value = htmlHelper.ViewData.Model == null
? default(TProperty)
: expression.Compile()(htmlHelper.ViewData.Model);
string selected = value == null ? String.Empty : value.ToString();
return htmlHelper.DropDownListFor(expression, createSelectList(expression.ReturnType, selected));
}
我从 This Link 那里得到了这个。在那篇文章的评论部分有人问了完全相同的问题..
我认为您需要将参数添加为 object htmlAttributes
并使用 htmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)
创建下拉列表。
您的完整帮助程序代码将变为:
public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression, object htmlAttributes )
where TModel : class
{
TProperty value = htmlHelper.ViewData.Model == null
? default(TProperty)
: expression.Compile()(htmlHelper.ViewData.Model);
string selected = value == null ? String.Empty : value.ToString();
return htmlHelper.DropDownListFor(expression, createSelectList(expression.ReturnType, selected), null, htmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
}
我发现在像您这样的情况下,最好的解决方案通常是查看 SelectExtensions
的实际源代码。