在asp.net核心菜单中添加活动类?

时间:2019-07-14 16:50:54

标签: jquery html css asp.net-mvc asp.net-core

这是我的菜单

<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>

并且此方法检查菜单处于活动状态

 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" : "";
    }

当我选择菜单时,它可以工作,但是当我单击一个游戏并看到详细信息已删除活动类时,此代码仅适用于索引视图,而不适用于详细信息菜单

3 个答案:

答案 0 :(得分:1)

在.Core 2.2 / 3.x中工作

1-以这种方式在Layout.cshtml中最后一行:

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

2-在_MenuPartial或另一个* .cshtml

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

3-根据标题中传递的ViewData设置ID

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

答案 1 :(得分:0)

您需要尝试此C#代码

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;
    }

在cshtml中,您需要进行更改

    <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>

答案 2 :(得分:0)

您必须只更改isactive方法,如下所示:

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" : "";
    }