I am making a component for formatting a list, this is an extension, I wrote the following code, but when it gives me an error at runtime:
Cannot convert lambda expression to type 'System.Web.WebPages.HelperResult' because it is not a delegate type
This is the extension:
public static MvcHtmlString FormatMyList<TModel, TValue>(this HtmlHelper<TModel> htmlHelper, IEnumerable<TValue> list, Expression<Func<TValue, System.Web.WebPages.HelperResult>> formatExp = null) { foreach (var item in list) { var itemFormated = formatExp.Compile().Invoke(item).ToString(); } return new MvcHtmlString(""); }
Call View:
var test = Html.FormatMyList<ModelType, ListType>(list, formatExp: x => @<text> This is format of @x.Cambio to test @x.Fala </text>);
I already tried to switch from HelperResult to dynamic, but did not work either.
I do not want to use only Func<object, HelperResult>
, as suggested in some posts in StackOverFlow, because there will be elements inside <text></text>
that must be strongly typed as ListType elements.
The format may be different in my views, so I can not use the template for ListType.
Is there any way to do this, even if not using an expression?
thanks
source share