无法在MVC 4中突出显示活动链接

时间:2014-04-05 08:45:41

标签: html asp.net asp.net-mvc asp.net-mvc-4 asp.net-mvc-helpers

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

MenuHelper Extension:

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标签将被分配活动class.how我可以这样做吗?我已经搜索过互联网,但无法通过任何方法解决这个问题。

1 个答案:

答案 0 :(得分: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());
    }