Asp.Net的mvc凸显当前页面链接技术? [英] Asp.Net Mvc highlighting current page link technique?

查看:113
本文介绍了Asp.Net的mvc凸显当前页面链接技术?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要亮菜单中的活动链接。我的菜单是顺便把母版页。我在寻找实现这一目标的最佳方式是什么?有任何想法吗?

I need to highlight active link in the menu. My menu is in the master page by the way. I'm looking for the best way to implement this? Any ideas?

推荐答案

要处理这种情况最好的办法是写一个HTML帮手。你可以使用 RouteData.Values​​ [行动] 来得到当前正在执行的行动,并比较菜单名称和它们是否匹配应用CSS类,将突出显示。

The best way to handle this is to write an HTML helper. You could use RouteData.Values["action"] to get the currently executing action and compare to the menu name and if they match apply a CSS class that will highlight it.

public static MvcHtmlString MenuItem(
    this HtmlHelper htmlHelper, 
    string action, 
    string text
)
{
    var menu = new TagBuilder("div");
    var currentAction = (string)htmlHelper.ViewContext.RouteData.Values["action"];
    if (string.Equals(
            currentAction, 
            action,
            StringComparison.CurrentCultureIgnoreCase)
    )
    {
        menu.AddCssClass("highlight");
    }
    menu.SetInnerText(text);
    return MvcHtmlString.Create(menu.ToString());
}

然后用这个帮手来渲染菜单项:

And then use this helper to render the menu items:

<%: Html.MenuItem("about", "About us") %>
<%: Html.MenuItem("contact", "Contact us") %>
...

这篇关于Asp.Net的mvc凸显当前页面链接技术?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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