Kris Oye Kris Oye - 4 months ago 25
C# Question

MVC Razor: How to reference helper parameter in Razor markup?

I am wanting to pass a template parameter to my helper method for rendering errors. I have seen several examples where this is done but appear to require that the variables in the template are in scope when the helper is invoked.

e.g. Expression of HelperResult to format item from a list

I am hoping to do something like:

public static MvcHtmlString ErrorBlock<TModel>(this HtmlHelper helper, TModel model, string @class = null, object context = null, string view = null, object attributes = null, Func<ErrorModel,HelperResult> errorTemplate = null)
where TModel : ErrorModel


@ShopMR.ErrorBlock(Model, errorTemplate: r => @<div>@r.Message</div>)

But I get the following compiler errors:

  1. Cannot convert lambda expression to intended delegate type because
    some of the return types in the block are not implicitly convertible
    to the delegate return type

  2. Cannot convert lambda expression to type 'HelperResult' because it is not a delegate type

I have tried creating a delegate but it results in the same error. Is this possible? Should my func return some other type that can be compiled/evaluated as Razor text?


To get rid of the compilation errors change the rasor to this :

@ShopMR.ErrorBlock(Model, errorTemplate:r => new HelperResult( x => { x.WriteLine($"<div>r.Message</div>" ); } )

The errorTemplate was expecting a lambda expression that returns HelperResult Template.