MVC3 助手不工作,未找到

发布于 2024-12-29 17:19:25 字数 2102 浏览 1 评论 0原文

我在 /Helpers 下的主目录中创建了以下帮助程序:

HtmlHelpers.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace Website.Helpers
{
    public static class HtmlHelpers
    {
        public static MvcHtmlString ActiveActionLinkHelper(this HtmlHelper Html, string text, string action, string controller, string activeClass = "active", bool actionCheck = false)
        {
            if (Html.ViewContext.RouteData.GetRequiredString("controller") == controller)
            {
                if (actionCheck)
                {
                    if (Html.ViewContext.RouteData.GetRequiredString("action") == action)
                        return Html.ActionLink(text, action, controller, new { Class = activeClass });
                }
                else
                {
                    return Html.ActionLink(text, action, controller, new { Class = activeClass });
                }
            }

            return Html.ActionLink(text, action, controller);
        }
    }
}

我将命名空间添加到了 Publishers Areas 文件夹中的 Views web.config 中:

  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.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="Website.Helpers" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

我不断收到此消息: 编译器错误消息:CS1061:“System.Web.Mvc.HtmlHelper”不包含“ActiveActionLink”的定义,并且找不到接受“System.Web.Mvc.HtmlHelper”类型的第一个参数的扩展方法“ActiveActionLink”(您是否缺少 using 指令或程序集引用?)

核心:@Html.ActiveActionLink("Dashboard", "Index", “仪表板”)

有谁知道我做错了什么?几乎没有任何关于如何或在何处存储 HTML 帮助器的教程。有人可以建议我吗?

I created the following helper in my main directory under /Helpers:

HtmlHelpers.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace Website.Helpers
{
    public static class HtmlHelpers
    {
        public static MvcHtmlString ActiveActionLinkHelper(this HtmlHelper Html, string text, string action, string controller, string activeClass = "active", bool actionCheck = false)
        {
            if (Html.ViewContext.RouteData.GetRequiredString("controller") == controller)
            {
                if (actionCheck)
                {
                    if (Html.ViewContext.RouteData.GetRequiredString("action") == action)
                        return Html.ActionLink(text, action, controller, new { Class = activeClass });
                }
                else
                {
                    return Html.ActionLink(text, action, controller, new { Class = activeClass });
                }
            }

            return Html.ActionLink(text, action, controller);
        }
    }
}

I added the namespace to the Views web.config in my Publishers Areas folder:

  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.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="Website.Helpers" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

I keep getting this message:
Compiler Error Message: CS1061: 'System.Web.Mvc.HtmlHelper' does not contain a definition for 'ActiveActionLink' and no extension method 'ActiveActionLink' accepting a first argument of type 'System.Web.Mvc.HtmlHelper' could be found (are you missing a using directive or an assembly reference?)

Core: @Html.ActiveActionLink("Dashboard", "Index", "Dashboard")

Does anyone know what I am doing wrong? There are hardly any tutorials on how or where to store an HTML helper. Can someone please advise me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终遇你 2025-01-05 17:19:25

核心:@Html.ActiveActionLink("Dashboard", "Index", "Dashboard") // 这里是问题所在

,因为你的方法是ActiveActionLinkHelper,你调用了不同的方法。

@Html.ActiveActionLinkHelper("Dashboard", "Index", "Dashboard") // try like this. 

Core: @Html.ActiveActionLink("Dashboard", "Index", "Dashboard") // here it is the problem

as your method is ActiveActionLinkHelper, your calling different method.

@Html.ActiveActionLinkHelper("Dashboard", "Index", "Dashboard") // try like this. 
゛时过境迁 2025-01-05 17:19:25

编译器是对的。您的方法名为ActiveActionLinkHelper。将其更改为 ActiveActionLink 一切都应该很好

The compiler is right. You're method is named ActiveActionLinkHelper. Change it to ActiveActionLink and all should be well

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文