Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC: Custom Html Helpers in Razor

I am having difficulty with Html Helpers when used with Razor. Said helpers worked fine in MVC 2 with the web form view engine. But not in razor. The error I get at runtime is:

Compiler Error Message: CS1502: The best overloaded method match for 'System.Web.WebPages.WebPageExecutingBase.Write(System.Web.WebPages.HelperResult)' has some invalid arguments  Source Error:   Line 1:  @using Wingspan.Web.Mvc; Line 2:  @Html.IncrementalMenu(MenuBlock.Site) 

Expanding the Show Detailed Compiler Output reveals:

d:\...\Views\Shared\MenuTop.cshtml(2,1): error CS1502: The best overloaded method match for 'System.Web.WebPages.WebPageExecutingBase.Write(System.Web.WebPages.HelperResult)' has some invalid arguments d:\...\Views\Shared\MenuTop.cshtml(2,7): error CS1503: Argument 1: cannot convert from 'void' to 'System.Web.WebPages.HelperResult' 

That indicates to me that razor doesn't like my helper, IncrementalMenu, returning void (which works fine in MVC 2 web form engine views).

I get no errors at Compile time, although the line of code (@Html.IncrementalMenu(...)) is red underlined with the following message:

Cannot implicitly convert type 'void' to 'object' 

IncrementalMenu is in the Wingspan.Web.Mvc namespace. It's signature is as follows:

public static void IncrementalMenu(this HtmlHelper html, MenuBlock menuBlock) {     // Uses an HtmlTextWriter to render a menu from the sitemap } 

I'm blowed if I know what is wrong...

PS:

The MenuBlock parameter is just an enum that identifies how the menu should render. Don't fixate on this as that is fine.

like image 366
awrigley Avatar asked Nov 18 '10 15:11

awrigley


People also ask

How can create HTML helper in ASP.NET MVC?

There are two ways in MVC to create custom Html helpers as below. We can create our own HTML helper by writing extension method for HTML helper class. These helpers are available to Helper property of class and you can use then just like inbuilt helpers. Add new class in MVC application and give it meaningful name.

Can we create custom HTML helper?

Creating HTML Helpers with Static MethodsThe easiest way to create a new HTML Helper is to create a static method that returns a string. Imagine, for example, that you decide to create a new HTML Helper that renders an HTML <label> tag. You can use the class in Listing 2 to render a <label> .

How can create HTML helper in core in asp net?

To create a custom HTML helper you have create a static class and static method. below example is for a custom HTML helper for submit button. Make sure you add below using statements. Now, You can now use it on the page where you want to define a button.

How are inline HTML helpers defined in a Cshtml file to be used in the same view?

These are the type of helpers that are used on a single view and are used on the same page. inline HTML helpers can be created using @helper tag. <!


1 Answers

You can call your helper like this:

@{ Html.IncrementalMenu(MenuBlock.Site); } 

WebForms syntax

<% Html.IncrementalMenu(MenuBlock.Site); %> 

You just call your method, and the return value (if there is any) is ignored.

Code like this expects a return value, and writes the return value to the html stream:

@Html.YourHelper() 

Webforms syntax:

<%: Html.YourHelper() %> 

The same, if result value != IHtmlString:

<%= Server.HtmlEncode(Html.YourHelper()) %> 
like image 165
GvS Avatar answered Sep 18 '22 15:09

GvS