2

几天前我发现了 NHaml,这是一个很棒的项目。

当我尝试使用 MVC2 Html 助手时,如Html.LabelFor()Html.TextBoxFor();视图不会编译。

例子:

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();

我试图在 naml 的 Web.config 部分中添加程序集和命名空间,但它并没有改变任何东西。

我在用着 :

  • 系统.Web.Mvc 2.0
  • .NET 框架 3.5 SP1
  • 来自 git trunk 的 Nhaml 1.5.0.2(并尝试了其他构建)

我的 NHaml 配置是:

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

3 回答 3

1

问题是视图类包含一个非泛型 HtmlHelper。或者一些新的扩展方法需要 ViewData.Model 的类型。

要更正此问题,请更改 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 );

重建和使用:)

于 2010-07-18T14:49:29.777 回答
1

看起来您遇到了程序集引用问题。

您可能引用的是 MVC 1.0 程序集,而不是 2.0 程序集?

于 2010-07-11T00:49:21.147 回答
0

据我所知,不支持新的 MVC 助手,实际上只有有限数量的 HtmlHelper 是 LinkExtensions。作为一个疯狂的猜测,您可以尝试将 LabelExtensions 添加到NHaml.Web.Mvc/NHamlMvcViewEngine.cs文件中 NHaml 视图引擎的设置中(因为您确实有源代码)并检查它是否有效。

private void InitializeTemplateEngine()
{

 // snip
_templateEngine.Options.AddReference( typeof( LabelExtensions ).Assembly.Location ); // Line 50
}
于 2010-07-12T07:22:37.033 回答