Using the MVC HtmlHelper Extension Method in a View Within a Scope

I have an HtmlHelper extension method that retrieves localized text from a database cache. The code is this. (MVCWeb is the namespace of my MVC application.)

using System.Web; using System.Web.Mvc; namespace MVCWeb.PresentationExtensions { public static class HtmlHelperExtensions { public static HtmlString GetText(this HtmlHelper Html, string keyword) { // code to get the text based on the keyword } } } 

I am using @using MVCWeb.PresentationExtensions in my views. In the ~ / Views folder, calling the extension method works fine.

I recently added an area. I use the extension method in the view files in the ~ / Areas / AreaName / Views folder and the code compiles and it works, however I get errors in the IDE.

Each time I use @Html.GetText("SomeKeyword") from the Area view, the following two errors appear in the error list.

  • 'System.Web.WebPages.Html.HtmlHelper' does not contain a definition for GetText and the best extension overload method. MVCWeb.PresenationExtension.HtmlHelperExtensions.GetText (System.Web.Mvc.HtmlHelper, string) 'has some invalid arguments
  • Instance argument: cannot be converted from 'System.Web.WebPages.Html.HtmlHelper' to 'System.Web.Mvc.HtmlHelper'

I realized that in ~ / Views, @Html has the following code comments:

 HtmlHelper<dynamic> WebViewPage<dynamic>.Html Gets or sets the System.Web.Mvc.HtmlHelper object that is used to render HTML elements. 

In ~ / Area / AreaName / Views, @Html has the following comments:

 HtmlHelper WebPage.Html Gets the System.Web.WebPages.Html.HtmlHelper object that is associated with a page. 

For reference, my Web.config files in ~ / Views and ~ / Areas / AreaName / Views correspond. This is an MVC4 application on .NET 4.5 and has not been converted from a previous version of MVC.

  • Is it normal that @Html is defined as different types in regular vs area views?
  • Why is this compilation and work correctly if the IDE shows errors? Is this an IDE bug?
  • How can I stop these errors from showing in the IDE?
+4
source share
2 answers

it works fine because your web.config contains the correct link and matches it at runtime correctly.

this is just a mistake for areas. To get rid of it, you can tell it how to enable it at the top of your view using @include, which will give intellisense a helping hand.

+1
source

I just ran into the same problem while trying to add the MvcSiteMap helper to the view in the area.

The problem was that the NuGet package added its namespaces to Web.Config files at the root and Views level, but it is not surprising that it was not smart enough to look for those who are buried in the Areas / area_name / Views folders. The solution was to simply add namespaces, for example.

 <system.web.webPages.razor> <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.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.Optimization" /> <add namespace="System.Web.Routing" /> <add namespace="FarmingtonCo.CacPortalWeb" /> <add namespace="MvcSiteMapProvider.Web.Html" /> <add namespace="MvcSiteMapProvider.Web.Html.Models" /> </namespaces> </pages> 

+1
source

All Articles