6
votes

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 definition 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?

2

2 Answers

18
votes

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);
5
votes

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.")