在Asp.NET MVC亮点导航菜单? [英] Navigation menu with highlight in Asp.NET MVC?

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

问题描述

这是一个简单的问题。计算器是怎么做他们的菜单在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.

推荐答案

有关这样做的目的,我已经writen一些code下来,有一些部分是用我的自定义扩展语言一样,继续使用它,只是忽略了次要角色。

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.

这一个我把我的部分包含菜单来获得的动作和控制器,这样我可以把它传递给扩展之上。

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(); %>

这是侧边栏项目,基本上这将生成一个链接,您的自定义类礼的标签,以表明是否链接在页面/高亮当前使用。

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>");
}

和这里是code以上

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

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

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