Why can't Razor find my HTML helper?

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

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

The helper method works great when used in regular (non-Razor) representations.

When used in a Razor view:

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

I get a runtime error message that

'System.Web.Mvc.HtmlHelper' does not contain a definition for 'WrappedValidationSummary' and there is no extension method 'WrappedValidationSummary' takes the first argument of the type 'System.Web.Mvc.HtmlHelper' (you do not see the link using the directive or assembly?)

The Razor validation syntax in Visual Studio and Intellisense has no problem finding an extension method definition. Recompiling the project does not help.

What is going wrong?

+6
asp.net-mvc-3 razor
source share
2 answers

Have you added helper 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 use RC, if you are in an early beta, you will need to add a namespace on the page or Global.asax.

In addition, I would suggest changing the return type to HtmlString .

 return new HtmlString(STRING_VALUE); 
+18
source share

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

 @using your_current_web_namespace . . . @Html.WrappedValidationSummary("Mitarbeiter konnnte nicht angelegt werden.") 
+5
source share

All Articles