2

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">
SandRock
  • 5,276
  • 3
  • 30
  • 49
  • Does the standard Html.Label method work? – Ahmad Jul 12 '10 at 07:15
  • It seems extension methods with arguments of type Expression<> are the only missing methods. Could this just be a missing namespace? And I just found that the standard HtmlHelper with the standard view engine is generic; NHaml one is not! Yeah ok, I found it. Let me push this for some tests and I'll answer myself. Thank you guys. – SandRock Jul 18 '10 at 14:45

3 Answers3

1

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
  • 8,029
  • 11
  • 48
  • 56
  • 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().Assembly The output is "System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" – SandRock Jul 11 '10 at 13:02
  • 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 Jul 12 '10 at 10:07
  • Did it. There is no MVC 1.0 assembly anywhere (it's not even installed on my machine). – SandRock Jul 18 '10 at 14:13
1

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
  • 5,276
  • 3
  • 30
  • 49
0

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
  • 22,657
  • 9
  • 52
  • 84
  • 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 Jul 18 '10 at 14:25