Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom HtmlHelper extension method usage problem in asp.net mvc razor

I have converted my classic asp.net mvc views to razor. In a view there is a usage of an extension method (it has overloads) of HtmlHelper:

@Html.CustomAction<AccountController, LogOnModel>("displayText", x => x.Register())

And CustomAction signature is:

public static HtmlString CustomAction<TController, TModel>(this HtmlHelper<TModel> view, string displayText, Expression<Func<TController, object>> where TController : Controller

I have also enabled view compilation at build time (through .proj file). When I build the project I get these errors pointing to that line:

  • Argument 1: cannot convert from 'method group' to 'System.Web.WebPages.HelperResult'

  • The best overloaded method match for 'System.Web.WebPages.WebPageExecutingBase.Write(System.Web.WebPages.HelperResult)' has some invalid arguments

What is the reason of these errors? How can I correct it?

like image 203
rovsen Avatar asked Feb 26 '23 01:02

rovsen


1 Answers

The Razor parser sees < and thinks it's an HTML tag. Therefore, it only parses Html.CustomAction as the expression.

You need to wrap the call in parentheses to force it to treat the entire call as a single expression:

@(Html.CustomAction<AccountController, LogOnModel>("displayText", x => x.Register()))
like image 186
SLaks Avatar answered Feb 27 '23 14:02

SLaks