Dimskiy
Dimskiy

Reputation: 5301

How to enable Intellisense in Html Helper with MVC3?

I basically followed this article: http://develoq.net/2011/how-to-create-custom-html-helpers-for-asp-net-mvc-3-and-razor-view-engine/

My HtmlHelper class is located in the root directory of my MVC project:

using System.Web.Mvc;

namespace MvcHtmlHelpers
{
    public static class HtmlHelperExtensions
    {
        public static MvcHtmlString Hello(this HtmlHelper helper)
        {
            return new MvcHtmlString("Hello!");
        }
    }
}

If I reference it in a view: @using MvcHtmlHelpers; I get intellisense and view code validates fine (no red underlines).

If I don't reference it in a View, but reference it in either one or both of the 2 web.config files, I don't get intellisense and @Html.Hello() is now red underlined.

Now the best part is that when I run, it renders perfectly fine even if I don't have it referenced anywhere at all. Ideally, I would like to reference it once in the web.config and get Intellisense in a view without referencing it in that view.

UPDATE: I guess something got cached somewhere. It does NOT render when it's not referenced anywhere. I'm sorry. I would still like to get Intellisense and validation in a view.

Upvotes: 0

Views: 1174

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039318

If you are using Razor view engine you need to reference it in ~/Views/web.config and not in ~/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="MvcHtmlHelpers" />
      </namespaces>
    </pages>
</system.web.webPages.razor>

Then recompile, close and reopen the Razor view and if it is your lucky day you might even get Intellisense. If not you may curse at Microsoft.

Upvotes: 3

Related Questions