Adding “active” tag to navigation list in an asp.net mvc master page

后端 未结 15 1918
渐次进展
渐次进展 2020-12-12 13:31

In the default asp.net mvc project, in the Site.Master file, there is a menu navigation list:

15条回答
  •  渐次进展
    2020-12-12 14:25

    An old question but hopefully someone might find this very helpful.

    1. Put some thing that you can use to identify your page in the ViewBag, I used ViewgBag.PageName

    For example, in index.cshtml, put something like

    @{
        ViewBag.PageName = "Index";
    }
    
    1. Add a class to each link item with a conditional statement to return active if the page being visited has the required value, or return an empty string otherwise. View below for details:

    I didn't just test it, I use this method in my projects

提交回复
热议问题