我尝试使用以下签名创建自定义HTML帮助器:
public static MvcHtmlString LookupDropDownListFor<TModel, TProperty, TIdProperty, TDisplayProperty, TExpiredProperty, TListItem>(
this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression,
IEnumerable<TListItem> enumeratedItems,
Expression<Func<TListItem, TIdProperty>> idProperty,
Expression<Func<TListItem, TDisplayProperty>> displayProperty,
Expression<Func<TListItem, TExpiredProperty>> expiredProperty,
string css = null,
string optionLabel = null,
string id = null,
object htmlAttributes = null)
{
//building the html
}
目前这样称呼:
@Html.LookupDropDownListFor(model => model.PrimaryName.NameSuffixId, Model.C_NameSuffixLst, id => id.Id, disp => disp.Display, exp => exp.IsVisible, "", "col-md-8")
这很好用。但是,现在我想创建一个没有idProperty,displayProperty和expiredProperty的重载。它需要使用三个默认值。
所以,我做了这个简化的重载:
public static MvcHtmlString LookupDropDownListFor<TModel, TProperty, TListItem>(
this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression,
IEnumerable<TListItem> enumeratedItems,
string css = null,
string optionLabel = null,
string id = null,
object htmlAttributes = null)
{
return LookupDropDownListFor(htmlHelper, expression, enumeratedItems, idProp => idProp.Id, disp => disp.Display, exp => exp.IsVisible, css, optionLabel, id, htmlAttributes);
}
但是,我在exp =&gt;上收到错误exp.IsVisible:
'TListItem' does not contain a definition for IsVisible
此时,它可以从哪里推断出TListItem的类型?这是为自定义HTML帮助程序重载的正确方法吗?