繁体   English   中英

活动菜单项 - asp.net mvc3母版页

[英]active menu item - asp.net mvc3 master page

我一直在扫描,试图找到一个合适的解决方案,为主页面中的菜单项分配“活动/当前”类。 关于是否执行此客户端与服务器端,该线路在中间分开。

说实话,我是JavaScript和MVC的新手,所以我没有意见。 我更愿意以“最干净”和最恰当的方式做到这一点。

我有以下jQuery代码将“活动”类分配给<li>项...唯一的问题是“索引”或默认视图菜单项将始终分配活动类,因为URL始终是子字符串其他菜单链接:

(default) index = localhost/
link 1 = localhost/home/link1
link 2 = localhost/home/link1

$(function () {
 var str = location.href.toLowerCase();
  $('#nav ul li a').each(function() {
   if (str.indexOf(this.href.toLowerCase()) > -1) {
    $(this).parent().attr("class","active"); //hightlight parent tab
   }
});

伙计们,有更好的方法吗? 有人至少会帮助我获得客户端版本的防弹吗? 那么“索引”或默认链接总是“活跃”? 有没有办法为索引方法分配假扩展? 喜欢而不仅仅是基本URL,它将是localhost/home/dashboard因此它不会是每个链接的子串?

说实话,我并没有真正遵循做这个服务器端的方法,这就是为什么我试图用jQuery做客户端...任何帮助将不胜感激。

自定义HTML帮助程序通常可以正常工作:

public static MvcHtmlString MenuLink(
    this HtmlHelper htmlHelper, 
    string linkText, 
    string actionName, 
    string controllerName
)
{
    string currentAction = htmlHelper.ViewContext.RouteData.GetRequiredString("action");
    string currentController = htmlHelper.ViewContext.RouteData.GetRequiredString("controller");
    if (actionName == currentAction && controllerName == currentController)
    {
        return htmlHelper.ActionLink(
            linkText,
            actionName,
            controllerName,
            null,
            new {
                @class = "current"
            });
    }
    return htmlHelper.ActionLink(linkText, actionName, controllerName);
}

并在您的母版页中:

<ul>
    <li>@Html.MenuLink("Link 1", "link1", "Home")</li>
    <li>@Html.MenuLink("Link 2", "link2", "Home")</li>
</ul> 

现在剩下的就是定义.current CSS类。

增加了对区域的支持:

public static class MenuExtensions
{
    public static MvcHtmlString MenuItem(this HtmlHelper htmlHelper, string text, string action, string controller, string area = null)
    {

        var li = new TagBuilder("li");
        var routeData = htmlHelper.ViewContext.RouteData;

        var currentAction = routeData.GetRequiredString("action");
        var currentController = routeData.GetRequiredString("controller");
        var currentArea = routeData.DataTokens["area"] as string;

        if (string.Equals(currentAction, action, StringComparison.OrdinalIgnoreCase) &&
            string.Equals(currentController, controller, StringComparison.OrdinalIgnoreCase) &&
            string.Equals(currentArea, area, StringComparison.OrdinalIgnoreCase))
        {
            li.AddCssClass("active");
        }
        li.InnerHtml = htmlHelper.ActionLink(text, action, controller, new {area}, null).ToHtmlString();
        return MvcHtmlString.Create(li.ToString());
    }
}

这是我对这个问题的解决方案。

我创建了HtmlHelpers类的以下扩展方法:

public static class HtmlHelpers
{
    public static string SetMenuItemClass(this HtmlHelper helper, string actionName)
    {
        if (actionName == helper.ViewContext.RouteData.Values["action"].ToString())
            return "menu_on";
        else
            return "menu_off";
    }

然后我有我的menublock。 它看起来像这样:

<div id="MenuBlock">
    <div class="@Html.SetMenuItemClass("About")">
        <a>@Html.ActionLink("About", "About", "Home")</a></div>
    <img height="31" width="2" class="line" alt="|" src="@Url.Content("~/Content/theme/images/menu_line.gif")"/>
    <div class="@Html.SetMenuItemClass("Prices")">
        <a>@Html.ActionLink("Prices", "Prices", "Home")</a></div>
</div>

所以我的方法根据Home控制器的当前动作将类名返回给每个div。 您可以更深入地添加方法一个参数,当您具有相同名称但具有不同控制器的操作时,该参数指定控制器的名称以避免出现问题。

通过JQuery你可以这样做:

$(document).ready(function () {
    highlightActiveMenuItem();
});

highlightActiveMenuItem = function () {
    var url = window.location.pathname;
    $('.menu a[href="' + url + '"]').addClass('active_menu_item');
};

.active_menu_item {
    color: #000 !important;
    font-weight: bold !important;
}

原文: http//www.paulund.co.uk/use-jquery-to-highlight-active-menu-item

我通常做的是将一个类分配给基于路径部分的body标签。 所以,如果你在路径上做一个String.Replace转/ blogs / posts / 1到class =“blogs posts 1”。

然后,您可以指定CSS规则来处理它。 例如,如果您有“博客”的菜单项,则可以执行类似的规则

BODY.blogs li.blogs { /* your style */}

或者如果你想要一个特定的风格,如果你在博客根页面上的帖子只有副作用

BODY.blogs.posts li.blogs {/* your style */}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM