0

我正在尝试突出显示 MVC 4 中的当前链接,但我不能这样做,我正在分享我到目前为止所做的事情

菜单助手扩展:

public static class MenuHelper
    {
        public static MvcHtmlString ListItemAction(this HtmlHelper helper, string name, string actionName, string controllerName)
        {
            var currentControllerName = (string)helper.ViewContext.RouteData.Values["controller"];
            var currentActionName = (string)helper.ViewContext.RouteData.Values["action"];
            var sb = new StringBuilder();

            sb.AppendFormat("<li{0}", (currentControllerName.Equals(controllerName, StringComparison.CurrentCultureIgnoreCase) &&
                                                currentActionName.Equals(actionName, StringComparison.CurrentCultureIgnoreCase)
                                                    ? " class=\"active\">" : ">"));
            var url = new UrlHelper(HttpContext.Current.Request.RequestContext);
            sb.AppendFormat("<a href=\"{0}\">{1}</a>", url.Action(actionName, controllerName), name);
            sb.Append("</li>");
            return new MvcHtmlString(sb.ToString());
        }
    }

这是我的 HTML

<li class="has-sub active">
            <a href="javascript:;" class="">
                <i class="icon-magnet"></i>Manage
                <span class="arrow open"></span>
                <span class="selected"></span>
            </a>
            <ul class="sub">
                @{


                        @Html.ListItemAction("Parts", "Index", "Part")
                        @Html.ListItemAction("Categories", "Index", "Category")
                        @Html.ListItemAction("Tickets", "Index", "Ticket")
                        @Html.ListItemAction("SearchLog", "Index", "SearchLog")
                        @Html.ListItemAction("Reviews", "Index", "Review")


                }



            </ul>

        </li>

这只是突出显示链接,但由于我的设计,我也想更改 LI 标记类。我还有其他链接,我希望当单击链接时,父 LI 标记将分配给活动类。我该怎么做?我已经搜索了互联网,但找不到任何方法来解决这个问题。

4

1 回答 1

0

这是我的菜单助手,其中包括嵌套链接,因此它突出显示活动链接和父菜单项。我认为您可以针对您的解决方案对其进行修改。

public static MvcHtmlString MenuItem(this HtmlHelper htmlHelper,
                                         MenuItem menuItem)
    {
        var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
        var li = new TagBuilder("li");
        var routeData = htmlHelper.ViewContext.RouteData;
        var currentAction = routeData.GetRequiredString("action");
        var currentController = routeData.GetRequiredString("controller");
        var isSelected = false;
        if (string.Equals(currentAction,
                          menuItem.item.Action,
                          StringComparison.OrdinalIgnoreCase) &&
            string.Equals(currentController,
                          menuItem.item.Controller,
                          StringComparison.OrdinalIgnoreCase) && menuItem.item.Action != "")
        {
            li.AddCssClass("start active");
            isSelected = true;

        }
        else if (menuItem.item.Action == "" && string.Equals(currentController,
                          menuItem.item.Controller,
                          StringComparison.OrdinalIgnoreCase))
        {
            li.AddCssClass("active open");
            isSelected = true;
        }

        var tag = new TagBuilder("a");

        tag.InnerHtml = "<i class=\"" + menuItem.item.Ikon + "\"></i> <span class=\"title\">" + menuItem.item.MenuYazisi + "</span>";
        if (isSelected) tag.InnerHtml += "<span class=\"selected\"></span>";
        if (menuItem.item.MenuEleman1.Count > 0)
        {
            if (isSelected)
            {
                tag.InnerHtml += "<span class=\"arrow open\"></span>";
            }
            else
            {
                tag.InnerHtml += "<span class=\"arrow \"></span>";

            }
        }

        tag.MergeAttribute("href",
            menuItem.childItems.Any()
                ? "javascript:;"
                : urlHelper.Action(menuItem.item.Action, menuItem.item.Controller));
        li.InnerHtml = tag.ToString();

        if (menuItem.childItems.Any())
        {
            li.InnerHtml += "<ul class=\"sub-menu\">";
            foreach (var item in menuItem.childItems)
            {
                var liTemp = new TagBuilder("li");
                if (string.Equals(currentAction,
                          item.Action,
                          StringComparison.OrdinalIgnoreCase) &&
                           string.Equals(currentController,
                          item.Controller,
                          StringComparison.OrdinalIgnoreCase))
                {
                    liTemp.AddCssClass("active");
                }
                var aTemp = new TagBuilder("a");
                aTemp.MergeAttribute("href", urlHelper.Action(item.Action, item.Controller));
                aTemp.InnerHtml = item.MenuYazisi;
                liTemp.InnerHtml += aTemp.ToString();
                li.InnerHtml += liTemp.ToString();
            }
            li.InnerHtml += "</ul>";
        }
        return MvcHtmlString.Create(li.ToString());
    }
于 2014-04-05T09:37:31.673 回答