3

I am creating my first mvc3 application (no previous mvc either) I am wanting to create a top level menu and be able to add a "selected" or "active" css class to the select LI tag. I found this link active menu item - asp.net mvc3 master page Ihave tried to add it to a class however I get an error back "Error 1 'System.Web.Mvc.HtmlHelper' does not contain a definition for 'ActionLink' and no extension method 'ActionLink' accepting a first argument of type 'System.Web.Mvc.HtmlHelper' could be found (are you missing a using directive or an assembly reference?) G:\MvcMusicStore-v2.0\MvcMusicStore-Completed\MvcMusicStore\Helpers\HtmlHelpers.cs 26 31 MvcMusicStore "

So I am thinking I am missing a reference to something however I don't know what. Can someone kindly tell me what I need to reference to get this to work.

Community
  • 1
  • 1
Diver Dan
  • 9,953
  • 22
  • 95
  • 166

1 Answers1

4

Add the following using at the top of your HtmlHelpers.cs file:

using System.Web.Mvc.Html;

Now the htmlHelper.ActionLink line should compile just fine.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Thanks Darin, I see you were the orginal person that suggested the code to Michael. I have created a helper class and added your example however I get this error Error 1 'System.Web.Mvc.HtmlHelper' does not contain a definition for 'MenuLink' and no extension method 'MenuLink' accepting a first argument of type 'System.Web.Mvc.HtmlHelper' could be found (are you missing a using directive or an assembly reference?) g:\MvcMusicStore-v2.0\MvcMusicStore-Completed\MvcMusicStore\Views\Shared\_Layout.cshtml MvcMusicStore Do I need to add a reference in my view to the helper class? – Diver Dan Jan 25 '11 at 18:46
  • 1
    @user293545, yes you need to reference the namespace that this helper has been declared into. You could do this directly in the View by adding an `@using` clause or better yet add it to the `~/Views/Web.config` inside the `` tag of the `` tag which will allow you to use the helper method in all razor views without having to explicitly declare it. – Darin Dimitrov Jan 25 '11 at 19:15