12

I am using VS Express 2012 for the Web. My project is a MVC 4 project.

I am trying to make a helper to create a menu item which is a png image with a label. I pretty much cut and pasted this code:

Action Image MVC3 Razor

When I try to use the helper razor can not find it.

I get this error:

CS1061: 'System.Web.Mvc.HtmlHelper' does not contain a definition for 'AddMenuItem' and no extension method 'AddMenuItem' accepting a first argument of type 'System.Web.Mvc.HtmlHelper' could be found (are you missing a using directive or an assembly reference?)

My code looks like this: http://pastebin.com/cfsN99tF

I am trying to use this from _Layout.cshtml: http://pastebin.com/5vN8yYCa

I have added the namespace to Web.Config

<pages>
  <namespaces>
    <add namespace="System.Web.Helpers" />
    <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="System.Web.WebPages" />
    <add namespace="HtmlHelperExtensions" />
  </namespaces>
</pages>

What else do I need to do?

Community
  • 1
  • 1
Anders E. Andersen
  • 1,635
  • 2
  • 14
  • 20
  • 3
    Which `web.config` have you edited? Because you need to add the namespace to the `web.config` which is inside the `Views` folder. You should also try to add the line: `@using HtmlHelperExtensions` to the top of your `_Layout.cshtml`... – nemesv Nov 12 '12 at 20:42
  • Thanks! I just needed to add it under the views/Web.config, and then it worked. I am pretty new to ASP.Net MVC so I didn't see there where two separate config files. Feel free to add that as an answer. It is also answered here: http://stackoverflow.com/questions/3239006/how-to-import-a-namespace-in-razor-view-page – Anders E. Andersen Nov 12 '12 at 21:36

2 Answers2

18

you also need to reload your solution

Dmitry Efimenko
  • 10,973
  • 7
  • 62
  • 79
14

It needs to be added to the config file in the Views folder, not the main config.

See this answer:

How do I import a namespace in Razor View Page?

Community
  • 1
  • 1
Anders E. Andersen
  • 1,635
  • 2
  • 14
  • 20