What happened to my HtmlHelper?

I created an Html extension method in the Helper class, but I cannot get it to work. I implemented it, as shown in various textbooks.

My static MenuItemHelper class:

public static string MenuItem(this HtmlHelper helper, string linkText, string actionName, string controllerName)
    {
        var currentControllerName = (string)helper.ViewContext.RouteData.Values["controller"];
        var currentActionName = (string)helper.ViewContext.RouteData.Values["action"];

        var sb = new StringBuilder();

        if (currentControllerName.Equals(controllerName, StringComparison.CurrentCultureIgnoreCase) && currentActionName.Equals(actionName, StringComparison.CurrentCultureIgnoreCase))
            sb.Append("<li class=\"selected\">");
        else
            sb.Append("<li>");

        sb.Append(helper.ActionLink(linkText, actionName, controllerName));
        sb.Append("</li>");
        return sb.ToString();
    }

import namespace

<%@ Import Namespace="MYAPP.Web.App.Helpers" %>

Implementation on my master.page

<%= Html.MenuItem("TEST LINK", "About", "Site") %> 

The error message I get is:

Method not found: 'System.String System.Web.Mvc.Html.LinkExtensions.ActionLink (System.Web.Mvc.HtmlHelper, System.String, System.String, System.String)

EDIT: It looks like the problem is with the application name. the folder is called MYAPP-MVC.Web, but in classes it translates to MYAPP_MVC.Web

I just tried this on a new application and it works

+3
source share
2 answers

ASP.NET MVCish 2.0. System.Web.Mvc.Html , ActionLink:

namespace MYAPP.Web.App.Helpers
{
    using System.Web.Mvc;
    using System.Web.Mvc.Html;

    public static class HtmlExtensions
    {
        public static MvcHtmlString MenuItem(this HtmlHelper helper, string linkText, string actionName, string controllerName)
        {
            var currentControllerName = (string)helper.ViewContext.RouteData.Values["controller"];
            var currentActionName = (string)helper.ViewContext.RouteData.Values["action"];

            var li = new TagBuilder("li");
            if (string.Equals(currentControllerName, controllerName, StringComparison.CurrentCultureIgnoreCase) &&
                string.Equals(currentActionName, actionName, StringComparison.CurrentCultureIgnoreCase))
            {
                li.AddCssClass("selected");
            }

            li.InnerHtml = helper.ActionLink(linkText, actionName, controllerName).ToHtmlString();
            return MvcHtmlString.Create(li.ToString());
        }
    }
}

, System.Web.Mvc.

+12

/. , .

<%@ Import namespace="Namespace.Where.Your.HtmlHelper.Extension.Is.Defined" %>

,

<pages> 
    <namespaces>
        <add namespace="Namespace.Where.Your.HtmlHelper.Extension.Is.Defined" />
    </namespaces>
</pages>

web.config.

EDIT:

, HtmlHelper.ActionLink() MvcHtmlString, string. , ToString() append StringBuilder. , MvcHtmlString, <%: ... %> , .

0

Source: https://habr.com/ru/post/1748743/


All Articles