ASP.NET MVC - Current Page highlighting in navigation

前端 未结 5 502
星月不相逢
星月不相逢 2020-12-08 11:46

I\'m wondering how is it possible to add a CSS Class to the current page in your navigation when using ASP.NET MVC 3? Here is my navigation in my _Layout.cshtml file:

<
5条回答
  •  盖世英雄少女心
    2020-12-08 12:05

    @{
       var controller = ViewContext.RouteData.Values["controller"].ToString();
       var action = ViewContext.RouteData.Values["action"].ToString();
       var isActiveController = new Func((ctrl, act, activeStyle, inactiveStyle) => controller == ctrl && action == act ? activeStyle : inactiveStyle);
     }
    

    Then in your class attribute in your HTML you can do:

    class="@isActiveController("controlername","action","activecssstyleclass","inactiveccsstyle")"
    

    Just an other way of @dknaack his answer.. bit more generic and less functionality to repeat in your code.

提交回复
热议问题