代码之家  ›  专栏  ›  技术社区  ›  SandRock Jerry Birchler

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

  •  2
  • SandRock Jerry Birchler  · 技术社区  · 16 年前

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

    当我尝试使用mvc2html助手时 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部分添加程序集和名称空间,但没有改变任何东西。

    我正在使用:

    • System.Web.Mvc 2.0版本

    我的NHaml配置是:

    <nhaml autoRecompile="true" templateCompiler="CSharp3" encodeHtml="false" useTabs="false" indentSize="2">
    
    3 回复  |  直到 16 年前
        1
  •  1
  •   Saajid Ismail    16 年前

    看起来您有程序集引用问题。

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

        2
  •  1
  •   SandRock Jerry Birchler    16 年前

    问题是视图类包含非泛型的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 );
    

    重建和使用:)

        3
  •  0
  •   Ahmad    16 年前

    据我所知,新的MVC助手不受支持,实际上只有有限的htmlhelper是LinkExtensions。作为一个粗略的猜测,您可以尝试将LabelExtensions添加到 NHaml.Web.Mvc/NHamlMvcViewEngine.cs

    private void InitializeTemplateEngine()
    {
    
     // snip
    _templateEngine.Options.AddReference( typeof( LabelExtensions ).Assembly.Location ); // Line 50
    }
    
    推荐文章