【问题标题】:Calling Html.ActionLink in a custom HTML helper在自定义 HTML 帮助器中调用 Html.ActionLink
【发布时间】:2010-03-04 13:08:09
【问题描述】:

我正在设计一个自定义 HTML 帮助程序,我想执行 Html.ActionLink 以提供动态 URL 生成。

    namespace MagieMVC.Helpers
    {
        public static class HtmlHelperExtension
        {
            public static string LinkTable(this HtmlHelper helper, List<Method> items)
            {
                string result = String.Empty;

                foreach (Method m in items)
                {
                    result += String.Format(
                        "<label class=\"label2\">{0}</label>" +
                        System.Web.Mvc.Html.ActionLink(...) +
                        "<br />",
                        m.Category.Name,m.ID, m.Name);
                }

                return result;
            }


  }
}

不幸的是,无论我尝试声明的命名空间如何,在此上下文中都无法识别 Html.ActionLink。

作为一个通用问题,我想知道在设计新的自定义助手时是否可以使用任何现有的标准/自定义 Html 助手方法。

谢谢。

【问题讨论】:

    标签: asp.net-mvc html-helper


    【解决方案1】:

    您还没有helper 吗?

    helper.ActionLink("text", "actionName");
    

    不要忘记包含 using System.Web.Mvc.Html 命名空间。

    是的,只要包含必要的命名空间,您就可以使用现有的扩展方法。

    【讨论】:

      【解决方案2】:

      仅供参考,对于 MVC 3,我在这里找到了 ActionLink:

      System.Web.Mvc.Html.LinkExtensions.ActionLink(text, action, controller).ToHtmlString();
      

      【讨论】:

      • 上面的第一个参数必须包含对htmlhelper的引用,见下文
      • public static MvcHtmlString ActionLinkCustom(this System.Web.Mvc.HtmlHelper html, string menutext, string action, string controller) .... string link = System.Web.Mvc.Html。 LinkExtensions.ActionLink(html,menutext, action, controller).ToHtmlString();
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多