4
votes

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

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 ~/Views folder, calling the extension method is working perfectly.

I recently added an Area. I am using the extension method in the View files in the ~/Areas/AreaName/Views folder, and the code is compiling and it does work, however I am getting errors in the IDE.

Every time I use @Html.GetText("SomeKeyword") from within the Area view, the following two errors are displayed on the errors list.

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

I've figured out 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 these 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 match. This is an MVC4 app on .NET 4.5 and hasn't been converted from a previous version of MVC.

  • Is it normal that @Html is defined as different types in the regular Views vs area Views?
  • Why is this compiling and running correctly if the IDE is showing errors? Is this an IDE bug?
  • How can I stop these errors from showing in the IDE?
2

2 Answers

1
votes

it runs fine because your web.config contains the correct reference and it matches up at runtime correctly.

it's just an ide bug for areas. To get rid of it you can specify it as an include at the top of your view using @include which will give intellisense a helping hand.

1
votes

I've just run into the same issue trying to add an MvcSiteMap helper to a View in an Area.

The problem was that the NuGet package added its namespaces to the Web.Config files at the root and Views level but unsurprisingly wasn't smart enough to go looking for the ones buried in the Areas/area_name/Views folders. The solution was just to add the namespaces e.g.

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