Html 使用 lambda 扩展模型的两个属性

Html Extension two properties from model with lambda

我需要调用一个 HTML 助手来传递模型中的两个属性。问题是当我尝试这个时:

 @Html.BsDropDownFor(x => x.Type.Id, x => x.Type.Descripcion, (IEnumerable<TextValue>)ViewBag.ClaseB)

header 方法定义如下:

  public static MvcHtmlString BsDropDownFor<TModel>(this HtmlHelper<TModel> htmlHelper,
      Expression<Func<TModel, TProperty>> expressionValue,
      Expression<Func<TModel, TProperty>> expressionText,
      IEnumerable<TextValue> items)

如果我定义

BsDropDownFor<TModel,TProperty> 

在两个参数中,期望做的事情相同 属性。

我应该如何定义接收两个属性的方法?

更新于 2015 年 3 月 3 日

好吧,我的扩展终于可以用了

更改了我的签名
  public static MvcHtmlString BsDropDownFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,
                                           Expression<Func<TModel, TProperty>> expression,
                                           string elementIdText,
                                           IEnumerable<TextValue> items)

expressionText 是为其他扩展方法建立的,在视图上被调用

@Html.BsDropDownFor(x => x.Type.Id, Html.ModelPropertyTagId( x => x.Type.Descripcion), (IEnumerable<TextValue>)ViewBag.ClaseB)

问候

听起来您需要为这两个属性设置单独的类型参数:

public static MvcHtmlString BsDropDownFor<TModel, TValue, TText>(
       this HtmlHelper<TModel> htmlHelper,
       Expression<Func<TModel, TValue>> expressionValue,
       Expression<Func<TModel, TText>> expressionText,
       IEnumerable<TextValue> items)
{
   ...
}