在 Asp.NET MVC 中突出显示导航菜单? [英] Navigation menu with highlight in Asp.NET MVC?

查看:19
本文介绍了在 Asp.NET MVC 中突出显示导航菜单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个简单的问题.stackoverflow 如何在 Asp.net MVC 中制作菜单,突出显示我们所在的页面.

It's a simple question. How did stackoverflow do their menu in Asp.net MVC, with highlight on what page we are on.

推荐答案

为此,我写了一些代码,有些部分使用我的自定义扩展,如语言,继续使用它,只是忽略次要部分.

For the purpose of this, i've writen some code down, there are some part that using my custom extension like Language, go ahead and use it, just ignore the minor part.

我将这个放在我的 Partial 之上,其中包含获取操作和控制器的菜单,以便我可以将其传递给扩展程序.

This one i place on top of my Partial that contain the menu to get the action and controller, so that i can pass this to the extension.

<%  string currentAction = ViewContext.RouteData.Values["action"].ToString();
    string currentController = ViewContext.RouteData.Values["controller"].ToString(); %>

这是侧边栏项目,基本上这将生成一个带有链接和您的自定义类的li"标签,以指示该链接当前是否在页面/突出显示中使用.

This is the sidebar Item, basically this will generate a "li" tag with a link and your custom class to indicate whether the link is currently used in the page / highlight.

public static string SidebarItem(this System.Web.Mvc.HtmlHelper html, string currentAction, string currentController, string action, string controller, string languageKey, params object[] args)
{
    TagBuilder tb = new TagBuilder("li");
    if (string.Equals(currentAction, action, StringComparison.OrdinalIgnoreCase) && string.Equals(currentController, controller, StringComparison.OrdinalIgnoreCase))
    {
        tb.GenerateId("activemenu");
    }
    string text = html.Language(languageKey, args);
    string link = html.ActionLink(text, action, controller).ToHtmlString();
    tb.SetInnerText("{0}");
    return String.Format(tb.ToString(), "<span>"+link+"</span>");
}

这是上面代码的实际用法

and here is the actual usage of the code above

<%= Html.SidebarItem(currentAction, currentController, "index", "home", "index") %>

这篇关于在 Asp.NET MVC 中突出显示导航菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆