Missing extension methods in HtmlHelper using NHaml

2.8k Views Asked by At

I discovered NHaml some days ago and it's a great project.

When I try to use MVC2 Html helpers like Html.LabelFor(), Html.TextBoxFor(); the views won't compile.

Example:

error CS1061: 'System.Web.Mvc.HtmlHelper' does not contain a definition for 'LabelFor' and no extension method 'LabelFor' accepting a first argument of type 'System.Web.Mvc.HtmlHelper' could be found (are you missing a using directive or an assembly reference?)
0185:         textWriter.Write("              ");
0185:         textWriter.Write(Convert.ToString(Html.LabelFor(model => model.Username)));
0187:         textWriter.WriteLine();

error CS1061: 'System.Web.Mvc.HtmlHelper' does not contain a definition for 'TextBoxFor' and no extension method 'TextBoxFor' accepting a first argument of type 'System.Web.Mvc.HtmlHelper' could be found (are you missing a using directive or an assembly reference?)
0194:         textWriter.Write("              ");
0194:         textWriter.Write(Convert.ToString(Html.TextBoxFor(model => model.Username)));
0196:         textWriter.WriteLine();

I tried to add assemblies and namespaces in the nhaml's Web.config section but it doesn't change anything.

I'm using :

  • System.Web.Mvc 2.0
  • .NET Framework 3.5 SP1
  • Nhaml 1.5.0.2 from git trunk (and tried other builds)

My NHaml configuration is:

<nhaml autoRecompile="true" templateCompiler="CSharp3" encodeHtml="false" useTabs="false" indentSize="2">
3

There are 3 best solutions below

0
On BEST ANSWER

The problem is the view class contains a non-generic HtmlHelper. Or some new extension methods requires the ViewData.Model's type.

To correct this problem, change the property and instantiation in NHaml.Web.Mvc/NHamlMvcView.cs.

//public HtmlHelper Html { get; protected set; } // line 42
public HtmlHelper<TModel> Html { get; protected set; }

//Html = new HtmlHelper( viewContext, this ); // line 37
Html = new HtmlHelper<TModel>( viewContext, this );

Rebuild and use :)

1
On

As far as I can see the new MVC helpers are not supported, actually only a limited amount of HtmlHelpers are namely LinkExtensions. As a wild guess, you can possibly try to adding the LabelExtensions to the setup of the NHaml viewengine in the NHaml.Web.Mvc/NHamlMvcViewEngine.cs file (since you do have the source) and check if that works.

private void InitializeTemplateEngine()
{

 // snip
_templateEngine.Options.AddReference( typeof( LabelExtensions ).Assembly.Location ); // Line 50
}
3
On

It looks like you have an assembly reference problem.

You are probably referencing the MVC 1.0 assemblies, instead of 2.0 assemblies?