views:

44

answers:

2

I've created a few html helper methods for asp.net mvc that I would like to use between projects. Does anyone know of a way to put them in their own project file so I can include that in other solutions?

I've tried this but it doesn't seem to work. When I try to use the helper it complains that htmlHelper is not being set. It isn't necessary to set this when the helper extension lives in the MVC project, so I'm clearly missing something.

A: 

If you want to use helper methods and avoid importing the namespace of your helper extension class in each of your views, you can register the namespace in your web.config and it will be accessible to all views.

Like this:

<add namespace="MyMvcNamespace.SomeLibrary"/>

Then you might want to put your HTML Helper extensions in a class library (called "SomeLibrary" in the "MyMvcNamespace" namespace), and add a reference to that in each MVC web application you want to utilize it in.

RPM1984
That part of it I was able to figure out. The problem is my SomeLibrary project compiles, but when I try to call that extension method the parameter identified as "this HtmlHelper htmlHelper" is causing throwing an error because it is never set. I suspect I'm probably missing a reference or a using statement somewhere, I just don't know where.
bteller
Have a look at this page: http://www.asp.net/mvc/tutorials/creating-custom-html-helpers-csThere are 2 known methods of creating custom html helpers - using extension method or regular static methods.Maybe the extension method needs to be in the same root namespace of the MVC web application (i know it shouldnt have to be though). Try putting the extension method in a standalone class in the same project as your web application. If that still doesnt work, try changing them from extension methods to regular static methods.As you say though, its probably something obvious (import, etc).
RPM1984
A: 

Okay, I'm feeling kinda silly about this, but it was something painfully obvious.

I had added references to System.Web.Extensions and System.Web.MVC. Apparently though it is really important to remember to add a reference for System.Web. Once I did that, good to go.

So, there you have it!

bteller