使用 NHaml 的 HtmlHelper 中缺少扩展方法
几天前我发现了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您似乎遇到了程序集引用问题。
您可能引用的是 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?
问题是视图类包含非通用 HtmlHelper。或者一些新的扩展方法需要 ViewData.Model 的类型。
要解决此问题,请更改 NHaml.Web.Mvc/NHamlMvcView.cs 中的属性和实例化。
重建并使用:)
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.
Rebuild and use :)
据我所知,新的 MVC 助手不受支持,实际上只有有限数量的 HtmlHelpers 是 LinkExtensions。作为一个疯狂的猜测,您可以尝试将 LabelExtensions 添加到 NHaml.Web.Mvc/NHamlMvcViewEngine.cs 文件中的 NHaml 视图引擎的设置中(因为您确实有源代码)并检查是否有效。
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.