29

I would like to use custom @Html.ActionLink

I am trying to use the following code:-

public static class LinkExtensions
{
    public static MvcHtmlString MyActionLink(
        this HtmlHelper htmlHelper, 
        string linkText, 
        string action, 
        string controller)
    {
        var currentAction = htmlHelper.ViewContext.RouteData.GetRequiredString("action");
        var currentController = mlHelper.ViewContext.RouteData.GetRequiredString("controller");

        if (action == currentAction && controller == currentController)
        {
          var anchor = new TagBuilder("a");
          anchor.Attributes["href"] = "#";
          anchor.AddCssClass("currentPageCSS");
          anchor.SetInnerText(linkText);
          return MvcHtmlString.Create(anchor.ToString());
         }

         return htmlHelper.ActionLink(linkText, action, controller);
    }
}

From Custom ActionLink helper that knows what page you're on

But I am getting

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?

Community
  • 1
  • 1
Umesh Sehta
  • 10,555
  • 5
  • 39
  • 68
  • 2
    if you are sure you have added namespace inside your config, you might have to close and reopen the file - check this answer http://stackoverflow.com/questions/4136703/razor-htmlhelper-extensions-not-found/4136773#4136773 – ssilas777 Oct 02 '12 at 15:01

6 Answers6

53

Add this using System.Web.Mvc.Html; on top of your file

krolik
  • 5,712
  • 1
  • 26
  • 30
18

Make sure you have the namespace for your extensions class included in your web.config. For example:

namespace MyProject.Extensions
{
    public static class LinkExtensions
    {
        //code
    }
}

In your site Web.config and/or Web.config located in your "Views" folder:

  <system.web>
    <pages>
      <namespaces>
        <add namespace="MyProject.Extensions" />
      </namespaces>
    </pages>
  </system.web>

Otherwise include a "using" block for the namespace at the top of your view page can work but for common namespaces I would do the above.

ASPX:

<%@ Import namespace="MyProject.Extensions" %>

RAZOR:

@using MyProject.Extensions
jamesmillerio
  • 3,154
  • 4
  • 27
  • 32
John Culviner
  • 22,235
  • 6
  • 55
  • 51
  • Ultimately the web.config portion of this answer is what I was missing. – Clarence Klopfstein Sep 08 '14 at 03:50
  • I had the same error, but that solution didn't work. This is in a RAZOR file. I tried @using Vidly.Extensions (I assume we're supposed to replace MyProject with our actual project name) but .Extensions is not in the autocomplete. – David Britz Mar 12 '20 at 18:22
15

Don't forget that the first parameter only accepts string. It will show you this error if it's NOT.

Crismogram
  • 906
  • 15
  • 27
  • 2
    Sure enough had to change first param type _int_ to _string_ then it was happy `@Html.ActionLink(row.Count.ToString(), "Dashboard", "Therapist"...)` – SushiGuy Sep 19 '20 at 18:01
7

Make sure that you have following using in your class file:

using System.Web.Mvc.Html;

This is needed because the HtmlHelper class is located in System.Web.Mvc namespace but the ActionLink extension method is located in System.Web.Mvc.Html namespace.

tpeczek
  • 23,867
  • 3
  • 74
  • 77
  • good call, that's included in the stock web.config but who knows if that is correct in this instance – John Culviner Oct 02 '12 at 20:29
  • @JohnCulviner The trick is that the question isn't precise about where the error is taking place. If it is the View then your answer is correct (View doesn't see the namespace so it needs to be added in web.config). If the error comes from extension class then it is using directive in the extension class file (web.config will not help here). I'm guessing here based on the fact that error speaks about `ActionLink` not `MyActionLink` method - we need to wait for author to clarify (hopefully he will come back here). – tpeczek Oct 03 '12 at 09:02
2

If your using nopcommerce add this using statement at the top of your view file.

@using Nop.Web.Framework.UI
chris c
  • 321
  • 2
  • 14
0

My issue was, I had incomplete syntax, "@Html.actionLink", in a view. Looks like I had started to add an action link and went a different direction but forgot to remove the partial action link, this caused the same error as above.... So check your syntax as that will throw the same runtime error. Good luck!

Al Nolan
  • 11
  • 1