views:

205

answers:

3

Hi, 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">
+1  A: 

It looks like you have an assembly reference problem.

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

Saajid Ismail
Both references in the NHaml project and in the Web project are System.Web.Mvc 2.0.0.0.To confirm, I added a simple code in a view:%p= Html.GetType().AssemblyThe output is "System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
SandRock
Delete all the assemblies from the '\BIN' folder, and try again. MVC will put the correct assemblies back in that folder when you do a build. The older assembly could still be in that folder, and it could be referencing it, somehow. Had a similar problem before with MVC 1.0 and other 3rd party assemblies.
Saajid Ismail
Did it. There is no MVC 1.0 assembly anywhere (it's not even installed on my machine).
SandRock
A: 

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
}
Ahmad
I already tried this with the latest git source.I think I'll dive deeper into the source to find how LinkExtensions are implemented.
SandRock
+1  A: 

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 :)

SandRock