Asp.Net Mvc highlighting current page link technique?

前端 未结 6 1170
误落风尘
误落风尘 2020-12-04 10:35

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?

6条回答
  •  忘掉有多难
    2020-12-04 10:53

    I use this solution:

    First - add attribute data-menuPageId to your menu links

    Next - add hidden field for current page Id to Layout.cshtml

    
    

    Add ViewBag.Page initializtion at your pages like Home or Products

    @{
        ViewBag.Page = "products";
    }
    

    And last thing you should reference this javascipt from yor Layout.cshtml

    $(function() {
        var pageIdAttr = "data-menuPageId";
        var currentPage = $("#currentPageId").attr("value");
    
        var menu = $(".menu");
    
        $("a[" + pageIdAttr + "]").removeClass("selected");
        $("a[" + pageIdAttr + "=\"" + currentPage + "\"]", menu).addClass("selected");
    });
    

提交回复
热议问题