MVC keep navigation active when redirect to "Add" page

798 Views Asked by At

I've created some pages using "Gentelella Alela" template, when I create pages, it has the ability to turn navigation active (with 'current-page' class) based on controller and action name:

enter image description here

I'm adding "Add" action in RoleController, when I redirect to "/Admin/Role/Add", the navigation active is gone:

enter image description here

Any idea how should I make it work?

The trigger button is coded this way: enter image description here

_Layout.cshtml navigation code: enter image description here

3

There are 3 best solutions below

0
On

Hope this will work for you.

<ul>
  <li class="@(ViewContext.RouteData.Values["Controller"].ToString() == "Home" ? "active" : "")">
    <a asp-area="" asp-controller="Home" asp-action="Index"><i class="icon fa fa-home"></i><span>Home</span></a>
  </li>
</ul>
0
On

One easy way to do is using BaseController

ViewData["ActivePage"] = ActivePage();

 private string ActivePage()
        {
            string actionName = this.ControllerContext
                .RouteData
                .Values["action"]
                .ToString();

            string controllerName = this.ControllerContext
                .RouteData
                .Values["controller"]
                .ToString();

            return string.Format($"{controllerName}-{actionName}");
        }

You can call active page before action executing. After this you need to check this ViewData on Layout page.

 $(document).ready(function () {
            var activePageName = '@ViewData["ActivePage"]';

            var activeElement = $('li.' + activePageName);
            activeElement.addClass('active');

            $.each(activeElement.parents('li'), function (k, v) {
                $(v).addClass('active');
            });
        });

And before script your ul and li element must contains specific name to recognize it. You need to use format given in BaseController ActivePage method ControlleName-ActionName.

<ul class="nav nav-second-level collapse">
                        <li class="Home-Index">
                            @Html.ActionLink("Index", "Index", "Home")
                        </li>
                    </ul>
0
On
public static class Utilities
    {
        public static string IsActive(this HtmlHelper html,
                                      string control,
                                      string action)
        {
            var routeData = html.ViewContext.RouteData;

            var routeAction = (string)routeData.Values["action"];
            var routeControl = (string)routeData.Values["controller"];

            // both must match
            var returnActive = control == routeControl &&
                               action == routeAction;

            return returnActive ? "current-page" : "";
        }
    }


<li class="treeview @Html.IsActive("Home", "Index") ">
<a href="@Url.Action("Index", "Dashboard", new { Area = "" })"><i class="fa fa-dashboard"></i> <span>Dashboard</span></a>
</li>