开发者

Why can't Razor find my HTML Helper?

开发者 https://www.devze.com 2023-01-31 05:36 出处:网络
I\'m trying to port a \"classic\" ASP.NET MVC view to Razor and got stuck when trying to use a traditional (non-Razor) Html helper method. The helper method has the following signature:

I'm trying to port a "classic" ASP.NET MVC view to Razor and got stuck when trying to use a traditional (non-Razor) Html helper method. The helper method has the following signature:

public static string WrappedValidationSummary(this HtmlHelper htmlHelper, string SummaryError)
{
...
}

The helper method does work fine when using it in regular (non-Razor) views.

When using it in the Razor view like this:

@Html.WrappedValidationSummary("Mitarbeiter konnnte nicht angelegt werden.");

I get a run-time error message that

'System.Web.Mvc.HtmlHelper' does not contain a definiti开发者_StackOverflow中文版on for 'WrappedValidationSummary' and no extension method 'WrappedValidationSummary' accepting a first argument of type 'System.Web.Mvc.HtmlHelper' could be found (are you missing a using directive or an assembly reference?)

The Razor syntax checker in Visual Studio and Intellisense have no problem finding my extension method's definition. Recompiling the project does not help.

What is going wrong?


Have you added the helper's namespace to your Views/web.config?

  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="CUSTOM_NAMESPACE" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

The above will only work if you're using an RC, if you're on an early Beta, you'll need to add the namespace in the page or Global.asax.

Also, I'd suggest changing the return type to HtmlString.

return new HtmlString(STRING_VALUE);


Alternatively you can use using in the first line of your view instead of Views/Web.config if you just want to use for specific view file.

@using your_current_web_namespace
.
.
.
@Html.WrappedValidationSummary("Mitarbeiter konnnte nicht angelegt werden.")
0

精彩评论

暂无评论...
验证码 换一张
取 消