使用 NHaml 的 HtmlHelper 中缺少扩展方法

发布于 2024-09-08 12:39:28 字数 1411 浏览 7 评论 0原文

几天前我发现了 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();

我尝试在 nhaml 的 Web.config 部分中添加程序集和命名空间,但它没有改变任何内容。

我正在使用:

  • 来自 git trunk 的System.Web.Mvc 2.0
  • .NET Framework 3.5 SP1
  • Nhaml 1.5.0.2 (并尝试了其他版本)

我的 NHaml 配置是:

<nhaml autoRecompile="true" templateCompiler="CSharp3" encodeHtml="false" useTabs="false" indentSize="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">

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

葬心 2024-09-15 12:39:28

您似乎遇到了程序集引用问题。

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

It looks like you have an assembly reference problem.

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

波浪屿的海角声 2024-09-15 12:39:28

问题是视图类包含非通用 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 );

重建并使用:)

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

醉酒的小男人 2024-09-15 12:39:28

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

private void InitializeTemplateEngine()
{

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

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文