I'm doing a component to format a list, it is an Extension, I wrote the following code, but, when in execution time, it gives me the error:

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("");
        }

View calling:

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 change from HelperResult to dynamic, but didn't work too.

I don't want to use only Func<object, HelperResult> as suggested in some posts in StackOverFlow, because, there will be items inside the <text></text>, that needs to be strongly typed as an item of ListType.

The format can be different in my views, so I can't use a template to ListType.

Is there a way to do that, even that not using the expression?

Thanks

Related posts

Recent Viewed