【问题标题】:MVC keep navigation active when redirect to "Add" page重定向到“添加”页面时,MVC 保持导航活动
【发布时间】:2017-12-29 13:21:14
【问题描述】:

我使用“Gentelella Alela”模板创建了一些页面,当我创建页面时,它能够根据控制器和操作名称激活导航(使用“当前页面”类):

我在 RoleController 中添加“添加”操作,当我重定向到“/Admin/Role/Add”时,导航活动消失了:

知道我应该如何让它工作吗?

触发按钮是这样编码的:

_Layout.cshtml 导航代码:

【问题讨论】:

  • 您如何指定“当前页面”。你也分享一下布局页面吗?
  • 当前页面类自带模板,更新了我的_Layout.cshtml帖子

标签: asp.net-mvc-5 url-action


【解决方案1】:

一个简单的方法是使用 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}");
        }

您可以在执行操作之前调用活动页面。在此之后,您需要在 Layout 页面上检查此 ViewData。

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

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

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

在脚本之前,你的 ul 和 li 元素必须包含特定的名称才能识别它。您需要使用 BaseController ActivePage 方法中给出的格式 ControlleName-ActionName。

<ul class="nav nav-second-level collapse">
                        <li class="Home-Index">
                            @Html.ActionLink("Index", "Index", "Home")
                        </li>
                    </ul>

【讨论】:

    【解决方案2】:
    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>
    

    【讨论】:

      【解决方案3】:

      希望这对你有用。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-03
        • 1970-01-01
        相关资源
        最近更新 更多