6

it's my menu

<nav class="moduletable navigation hidden-sm hidden-xs">
  <ul id="nav" class="sf-menu">
    <li class="@Html.IsActive("GameVideos", "Index")"><a href="@Url.Action("Index", "GameVideos")">videogame</a></li>                                
    <li class="@Html.IsActive("SystemRequirements", "Index")"><a href="@Url.Action("Index", "SystemRequirements")">systemReq</a></li>
    <li class="@Html.IsActive("Games", "UpcommingGames")"><a href="@Url.Action("UpcommingGames", "Games")">upcomming game</a></li>
  </ul>
</nav>

and this method check menu active

 public static string IsActive(this IHtmlHelper htmlHelper, string controller, string action)
    {
        var routeData = htmlHelper.ViewContext.RouteData;

        var routeAction = routeData.Values["action"].ToString();
        var routeController = routeData.Values["controller"].ToString();

        var returnActive = (controller == routeController && action == routeAction);

        return returnActive ? "active" : "";
    }

When I select menu it is working but when I click on one game and see details active class is removed, this code just worked for index view and but not working for details menu

1
  • game is a controller action or rendered in view or you are called any ajax function? Commented Jul 15, 2019 at 6:00

3 Answers 3

17

you must just change isactive method like this :

public static string IsActive(this IHtmlHelper htmlHelper, string controller, string action)
    {
        var routeData = htmlHelper.ViewContext.RouteData;

        var routeAction = routeData.Values["action"].ToString();
        var routeController = routeData.Values["controller"].ToString();

        var returnActive = (controller == routeController && (action == routeAction || routeAction == "Details"));

        return returnActive ? "active" : "";
    }
Sign up to request clarification or add additional context in comments.

Comments

4

Work in .Core 2.2 / 3.x

1- In make this way in Layout.cshtml, last lines:

@if (ViewData["Active"] != null)
{
    <script type="text/javascript">document.getElementById("@ViewData["Active"]").classList.add("active")</script>
}

2- In _MenuPartial or another *.cshtml

@{
    ViewBag.Title = "Super Administração";
    ViewData["Active"] = "Home";
}

3- Set the ID according to the ViewData passed in the header

<a id="Home" asp-action="Index" asp-controller="Super" class="nav-link">

Comments

3

you need to try this C# code

public static string IsActive(this IHtmlHelper html, string controller = null, string action = null, string cssClass = null)
    {

        if (String.IsNullOrEmpty(cssClass))
            cssClass = "active";

        string currentAction = (string)html.ViewContext.RouteData.Values["action"];
        string currentController = (string)html.ViewContext.RouteData.Values["controller"];

        if (String.IsNullOrEmpty(controller))
            controller = currentController;

        if (String.IsNullOrEmpty(action))
            action = currentAction;

        return controller.ToLower().Split(',').Contains(currentController.ToLower()) && action.ToLower().Split(',').Contains(currentAction.ToLower()) ?
            cssClass : String.Empty;
    }

In cshtml, you need to change

    <nav class="moduletable navigation hidden-sm hidden-xs">
  <ul id="nav" class="sf-menu">
    <li class="@Html.IsActive("GameVideos", "Index")"><a href="@Url.Action("Index", "GameVideos")">videogame</a></li>                                
    <li class="@Html.IsActive("SystemRequirements", "Index")"><a href="@Url.Action("Index", "SystemRequirements")">systemReq</a></li>
    <li class="@Html.IsActive("Games", "UpcommingGames,Details”)”><a href="@Url.Action("UpcommingGames”, "Games")">upcomming game</a></li>
  </ul>
</nav>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.