【问题标题】:Custom HtmlHelper (image inside an ActionLink)自定义 HtmlHelper(ActionLink 中的图像)
【发布时间】:2012-04-09 23:36:26
【问题描述】:

我已经看到了几种不同的方法,但不是我正在寻找的具体方法。我目前有一个自定义 HtmlHelper,它将“class='currentPage'”添加到 ActionLink 以突出显示导航系统中的当前页面。

public static MvcHtmlString MenuItem(
            this HtmlHelper htmlHelper,
            string text,
            string action,
            string controller
        )
        {
            string value;
            var routeData = htmlHelper.ViewContext.RouteData;
            var currentAction = routeData.GetRequiredString("action");
            var currentController = routeData.GetRequiredString("controller");
            if (string.Equals(currentAction, action, StringComparison.OrdinalIgnoreCase) &&
                string.Equals(currentController, controller, StringComparison.OrdinalIgnoreCase))
            {
                value = htmlHelper.ActionLink(text, action, new { controller = controller }, new { @class = "currentPage" }).ToHtmlString();
                return MvcHtmlString.Create(value.ToString());
            }

            value = htmlHelper.ActionLink(text, action, controller).ToHtmlString();
            return MvcHtmlString.Create(value.ToString());
        }

这很好用,但我有一个管理区域,其中还有一个与每个菜单项关联的图像。图片位于链接中,如下所示:

<li><a href="/Admin/Blog"><img src="/Content/images/icons/page_edit.png" alt="" /> Blog</a></li>

我想为“MenuItem”创建一个覆盖方法,将图像添加到 ActionLink 中,但我有点难过。目前我有以下,将img标签放在外面......

public static MvcHtmlString MenuItem(
            this HtmlHelper htmlHelper,
            bool isAdmin,
            string text,
            string action,
            string controller
        )
        {
            string value;
            var routeData = htmlHelper.ViewContext.RouteData;
            var currentAction = routeData.GetRequiredString("action");
            var currentController = routeData.GetRequiredString("controller");

            value = "<img src='/Content/images/admin_icons/" + text + ".png' alt='' /> ";

            if (string.Equals(currentAction, action, StringComparison.OrdinalIgnoreCase) &&
                string.Equals(currentController, controller, StringComparison.OrdinalIgnoreCase))
            {
                value += htmlHelper.ActionLink(text, action, new { controller = controller }, new { @class = "currentPage" }).ToHtmlString();

            }
            else
            {
                value += htmlHelper.ActionLink(text, action, controller).ToHtmlString();
            }

            return MvcHtmlString.Create(value.ToString());
        }

有什么想法吗?

【问题讨论】:

    标签: c# html css asp.net-mvc-3


    【解决方案1】:

    我会使用 UrlHelper 生成 URL,然后格式化字符串。我的示例不涉及添加条件类,但这应该很容易包含。

            var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
            var url = urlHelper.Action(action, controller);
            var imgUrl = urlHelper.Content("~/Content/images/icons/page_edit.png");
            var html = string.Format("<li><a href=\"{0}\"><img src=\"{2}\" alt=\"\" />{1}</a></li>", url, text, imgUrl);
            return new MvcHtmlString(html);
    

    【讨论】:

    • var html = string.Format("
    • {1}
    • ", 网址, 文字, imgUrl); “索引(从零开始)必须大于或等于零且小于参数列表的大小。”格式是否正确?
  • 想通了...没有看到数字被搞砸了。效果很好,谢谢!
  • 猜你喜欢
    相关资源
    最近更新 更多
    热门标签