Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why don't my HtmlHelper extensions work?

I'm building an ASP.Net MVC website. Rather than have everything in one project, I've decided to separate the Web, Model and Controller out into different projects in the same solution, that reference each-other.

The referencing goes like this:

Web ---[references]---> Controller ---[references]---> Model

Now I wanted to add 2 custom methods to the HtmlHelper class - they're called "IncludeScript" and "IncludeStyle". They each take a single string parameter, and generate a script or link tag respectively.

I've created an extender class, according to documentation on the web, and written the two methods and compiled the application.

Now, when I go into the Public.Master page (which is my main master-page, and one of the places where I intend to use these methods), I can enter code such as below:

<%= Html.IncludeScript("\js\jquery.js") %>

The IntelliSense picks up and IncludeScript method and shows me the syntax just fine. So I'd expect that everything should work.

But it doesn't.

Everything compiles, but as soon as I run the application, I get the following run-time error from line 14 of Default.aspx.cs:

c:\\Projects\\PhoneReel\\PhoneReel.Web\\Views\\Shared\\Public.Master(11): error CS0117: 'System.Web.Mvc.HtmlHelper' does not contain a definition for 'IncludeScript'

Here's the line of code that the error happens on:

httpHandler.ProcessRequest(HttpContext.Current);

Any ideas what could be going wrong here?

like image 494
Jonathan Avatar asked Oct 20 '08 02:10

Jonathan


People also ask

How to create a custom HTML Helper?

Creating HTML Helpers with Static Methods The 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> .

What is HTML Helpers?

An HTML Helper is just a method that returns a HTML string. The string can represent any type of content that you want. For example, you can use HTML Helpers to render standard HTML tags like HTML <input>, <button> and <img> tags etc.

What does HtmlHelper class do?

The HtmlHelper class renders HTML controls in the razor view. It binds the model object to HTML controls to display the value of model properties into those controls and also assigns the value of the controls to the model properties while submitting a web form.


3 Answers

Check to make sure that the namespace of your extensions is accessible to our view. You need either this in your view:

<%@ Import Namespace="MyRootNamespace.NamespaceForMyHtmlHelperExtensions"%>

or this in your web config namespaces section:

<add namespace="MyRootNamespace.NamespaceForMyHtmlHelperExtensions"/>
like image 66
Tim Scott Avatar answered Oct 19 '22 05:10

Tim Scott


If you're using strongly typed views, and your extension method is extending HtmlHelper<object>, it's not going to find the extension. You'd have to create a generic extender to extend HtmlHelper<T>.

public static string IncludeScript<T>(this HtmlHelper<T> html, string url) {
   return "<script type=\"text/javascript\" src=\"" + url + "\"></script>";
}

Then you'll see your extender method show up.

I hope that helps.

like image 26
Ben Lesh Avatar answered Oct 19 '22 07:10

Ben Lesh


Make sure to have an import directive to your extensions methods namespace in your page.

Otherwise, Visual Studio might be able to see but your website won't be able to.

like image 1
Maxime Rouiller Avatar answered Oct 19 '22 06:10

Maxime Rouiller