1

I've created an Area in my app called "Admin" and when I attempt to use @html.actionlink() to build some menu items I'm met with a reference error.

system.web.WebPages.Html.HtmlHelper does not contain a definition for ActionLink and the best extension method overload System.Web.Html.LinkExtensions.ActionLink(System.Web.HtmlHelper,string, string, string) has some invalid arguments.

This is the line of code that's generating the error.

@Html.ActionLink("Posts", "Index", new { area = "admin" }); 

I've googled around and attempted adding using system.web.mvc.html; with no benefit or change in behavior. I've checked the web.config file for the area and confirmed that both system.web.mvc and system.web.mvc.html are included in the namespaces, also to no avail.

5
  • Do you have a web.config in /Areas/Admin/Views? Commented Jul 31, 2014 at 15:51
  • DavidG. I do. I checked it and the proper namespaces are all there. Commented Jul 31, 2014 at 16:33
  • I vaguely remember encountering this problem. Try restarting studio. I seem to remember there being an SO post with that little tidbit, and that's what did the trick for me (stupid I know). Commented Jul 31, 2014 at 17:10
  • Thanks mike. I'll check that. Commented Jul 31, 2014 at 18:29
  • Check that you're using the correct overload for HtmlHelper.ActionLink Commented Jul 31, 2014 at 18:50

2 Answers 2

1

You are using actionlink wrongly.

Below is the correct usage of actionlink(i m taking example of any arbitrary actionlink)

Html.ActionLink(article.Title,   // <--Link Text
            "Item",   // <-- ActionMethod
            "Login",  // <-- Controller Name.
            new { id = article.ArticleID }, // <-- Route arguments.
            null  // <-- htmlArguments 
            )

This uses the following method ActionLink signature:

public static string ActionLink(this HtmlHelper htmlHelper, 
                            string linkText,
                            string actionName,
                            string controllerName,
                            object values, 
                            object htmlAttributes)

The error is coming in your question because 3rd overload of Actionlink is controller name.

@Html.ActionLink("// Link Text //", "// Action Name //",// controller name //, new { area = "admin" });
Sign up to request clarification or add additional context in comments.

2 Comments

I believe the option he is using is Controller, Action, Options. So Posts is his controller name
Correct. Posts is the controller. Action is index
0

You are missing a web.config in your /Areas/Admin/Views folder. Just create the basic one like below, the key part being the added namespaces:

<?xml version="1.0"?>

<configuration>
  <configSections>
    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
    </sectionGroup>
  </configSections>

  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.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="System.Web.Optimization" />
        <add namespace="Club.Web" />
        <add namespace="Club.Web.Helpers"/>

      </namespaces>
    </pages>
  </system.web.webPages.razor>

  <appSettings>
    <add key="webpages:Enabled" value="false" />
  </appSettings>

  <system.webServer>
    <handlers>
      <remove name="BlockViewHandler"/>
      <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
    </handlers>
  </system.webServer>
</configuration>

1 Comment

I have a web.config and it accounts for mvc.html and the others. Am I missing something?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.