表达HelperResult以从列表中格式化项目

我正在做一个组件来格式化一个列表,它是一个扩展,我写了下面的代码,但是,在执行时,它给了我错误:

无法将lambda表达式转换为类型’System.Web.WebPages.HelperResult’,因为它不是委托类型

这是扩展名:

public static MvcHtmlString FormatMyList(this HtmlHelper htmlHelper, IEnumerable list, Expression<Func> formatExp = null) { foreach (var item in list) { var itemFormated = formatExp.Compile().Invoke(item).ToString(); } return new MvcHtmlString(""); } 

查看电话:

 var test = Html.FormatMyList(list, formatExp: x => @ This is format of @x.Cambio to test @x.Fala ); 

我已经尝试从HelperResult更改为动态,但也没有工作。

我不想像StackOverFlow中的一些post中那样只使用Func ,因为会有一些项,需要强类型化为ListType项。

我的视图中的格式可能不同,因此我无法将模板用于ListType。

有没有办法做到这一点,即使不使用表达式?

谢谢

我做到了,而不是使用表达式Expression> ,我只使用了一个Func:

 Func 

视图:

 var test = Html.FormatMyList(list, format: @ This is format of @item.Cambio to test @item.Fala ); 

我使用“item”键来访问LisType属性。

使用表达式的唯一原因是访问强类型的属性,因为我可以使用“item”键,我不再需要Expression。

这对我有用,谢谢。