On the following html and extension method I’m getting the following error: ‘No overload for method ‘MenuLink’ takes 3 arguments’
I didn’t think you needed to pass in a parmeter for the ‘this’ parm.
Thanks
public static class HTMLHelper
{
public static MvcHtmlString MenuLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName)
{
var routeData = htmlHelper.ViewContext.RouteData;
string currentAction = routeData.GetRequiredString("action");
string currentController = routeData.GetRequiredString("controller");
if (actionName == currentAction && controllerName == currentController)
{
return htmlHelper.ActionLink(linkText, actionName, controllerName, null, new { @class = "selected" });
}
return htmlHelper.ActionLink(linkText, actionName, controllerName);
}
}
}
<ul>
<li id="tab10">@HunterEdge.Web.Helper.HTMLHelper.MenuLink("Overview", "Index", "Statistics")</li>
<li id="tab20">@HunterEdge.Web.Helper.HTMLHelper.MenuLink("Detail View", "Detail", "Statistics")</li>
<li id="tab30">@HunterEdge.Web.Helper.HTMLHelper.MenuLink("Trends", "Trends", "Statistics")</li>
</ul>
In the original example that I provided I was calling the extension method directly on Html:
You could also call the method directly as you are trying but you need to provide the html helper as first argument:
which obviously is extremely ugly and it is not how extension methods, which is what HTML helpers are, were supposed to work.
The reason you might be getting an error if you was using the first syntax is because you didn’t bring the extension method in scope by putting
@using HunterEdge.Web.Helperon the top of your view or adding this namespace to the<namespaces>section of~/Views/web.config.