Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Web.Mvc.HtmlHelper' does not contain a definition for 'ActionLink'

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?

like image 797
Mohit Kumar Avatar asked Oct 02 '12 14:10

Mohit Kumar


4 Answers

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

like image 98
krolik Avatar answered Nov 12 '22 03:11

krolik


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
like image 20
John Culviner Avatar answered Nov 12 '22 04:11

John Culviner


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

like image 11
Crismogram Avatar answered Nov 12 '22 04:11

Crismogram


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.

like image 7
tpeczek Avatar answered Nov 12 '22 05:11

tpeczek