设置菜单中的当前、当前之前和当前元素之后

发布于 2024-11-19 09:00:20 字数 1934 浏览 1 评论 0原文

对于设置,我使用 Html 辅助方法,这在我看来不是最好的,因为我使用静态字段。

public enum CurrentState
{
    BeforeCurrent,
    AfterCurrent
}

public static CurrentState currentState = CurrentState.BeforeCurrent;

public static MvcHtmlString ActiveActionLink(this HtmlHelper helper, string linkText, string actionName, string controllerName, bool checkAction = true)
{
    string currentAction = helper.ViewContext.RouteData.GetRequiredString("action");
    string currentController = helper.ViewContext.RouteData.GetRequiredString("controller");

    if ((controllerName == currentController) && checkAction && (actionName == "Index"))
    {
        currentState = CurrentState.BeforeCurrent;
    }

    if ((controllerName == currentController) && checkAction && (actionName != currentAction))
    {
        if (currentState == CurrentState.BeforeCurrent)
        {
            return helper.ActionLink(linkText, actionName, controllerName, null, new { @class = "beforeCurrent" });
        }
        else if (currentState == CurrentState.AfterCurrent)
        {
            return helper.ActionLink(linkText, actionName, controllerName, null, new { @class = "afterCurrent" });
        }
    }

    if ((controllerName == currentController) && (!checkAction || (actionName == currentAction)))
    {
        currentState = CurrentState.AfterCurrent;
        return helper.ActionLink(linkText, actionName, controllerName, null, new { @class = "current" });
    }

    return helper.ActionLink(linkText, actionName, controllerName);
}

我有两级菜单,这就是我使用 checkAction 参数的原因:

  • 主菜单 - @Html.ActiveActionLink(Resources.Global.mainMenuBoard, "Index", "Board", checkAction: false)
  • 侧菜单 - @Html.ActiveActionLink(@Resources .Global.managementOverview、“索引”、“管理”)

和侧面菜单中我需要知道它是否在当前之后和之前(重叠项目...)。

这是一种改进的方法吗? 另外我必须说我也使用 javascript 来实现这一点,但它也必须适用于禁用 javascript 的情况。

For setting that I use Html helper method which is not the best imo, because I use static field.

public enum CurrentState
{
    BeforeCurrent,
    AfterCurrent
}

public static CurrentState currentState = CurrentState.BeforeCurrent;

public static MvcHtmlString ActiveActionLink(this HtmlHelper helper, string linkText, string actionName, string controllerName, bool checkAction = true)
{
    string currentAction = helper.ViewContext.RouteData.GetRequiredString("action");
    string currentController = helper.ViewContext.RouteData.GetRequiredString("controller");

    if ((controllerName == currentController) && checkAction && (actionName == "Index"))
    {
        currentState = CurrentState.BeforeCurrent;
    }

    if ((controllerName == currentController) && checkAction && (actionName != currentAction))
    {
        if (currentState == CurrentState.BeforeCurrent)
        {
            return helper.ActionLink(linkText, actionName, controllerName, null, new { @class = "beforeCurrent" });
        }
        else if (currentState == CurrentState.AfterCurrent)
        {
            return helper.ActionLink(linkText, actionName, controllerName, null, new { @class = "afterCurrent" });
        }
    }

    if ((controllerName == currentController) && (!checkAction || (actionName == currentAction)))
    {
        currentState = CurrentState.AfterCurrent;
        return helper.ActionLink(linkText, actionName, controllerName, null, new { @class = "current" });
    }

    return helper.ActionLink(linkText, actionName, controllerName);
}

I have two levels of menus and that's why I use checkAction parameter:

  • main menu - @Html.ActiveActionLink(Resources.Global.mainMenuBoard, "Index", "Board", checkAction: false)
  • side menu - @Html.ActiveActionLink(@Resources.Global.managementOverview, "Index", "Management")

and in side menu I need to know if it's after and before current (overlapping items...).

Is it a way to improve that?
Additionally I must say that I use javascript also for that but it must work also for javascript disabled.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故事和酒 2024-11-26 09:00:20

我最终通过在一个助手中生成整个菜单来解决这个问题:

public class Link
{
    public string LinkText { get; set; }
    public string ActionName { get; set; }
}

public static List<MvcHtmlString> SubMenuLinks(this HtmlHelper helper, string controllerName, List<Link> links)
{
    List<MvcHtmlString> menuElements = new List<MvcHtmlString>();
    string actualCssClass = "beforeCurrent";

    string currentAction = helper.ViewContext.RouteData.GetRequiredString("action");
    string currentController = helper.ViewContext.RouteData.GetRequiredString("controller");

    foreach (Link link in links)
    {
        if (controllerName == currentController && link.ActionName == currentAction)
        {
            menuElements.Add(helper.ActionLink(link.LinkText, link.ActionName, controllerName, null, new { @class = "current" }));
            actualCssClass = "afterCurrent";
        }
        else
        {
            menuElements.Add(helper.ActionLink(link.LinkText, link.ActionName, controllerName, null, new { @class = actualCssClass }));
        }
    }

    return menuElements;
}

并在视图中:

@{
    List<MvcHtmlString> actionMenu = Html.SubMenuLinks("Manager", new List<Link>()
    {
        new Link() { LinkText = "linkText", ActionName = "actionName" },
        new Link() { LinkText = "linkText2", ActionName = "actionName2" }
    });
}
@section SideMenu
{
    @for (int i = 0; i < actionMenu.Count; i++) 
    { 
        <li id="menu@(i)">@actionMenu.ElementAt(i)</li>
    }
}

不完美,但至少可以工作。

I finally solve that by generating whole menu in one helper:

public class Link
{
    public string LinkText { get; set; }
    public string ActionName { get; set; }
}

public static List<MvcHtmlString> SubMenuLinks(this HtmlHelper helper, string controllerName, List<Link> links)
{
    List<MvcHtmlString> menuElements = new List<MvcHtmlString>();
    string actualCssClass = "beforeCurrent";

    string currentAction = helper.ViewContext.RouteData.GetRequiredString("action");
    string currentController = helper.ViewContext.RouteData.GetRequiredString("controller");

    foreach (Link link in links)
    {
        if (controllerName == currentController && link.ActionName == currentAction)
        {
            menuElements.Add(helper.ActionLink(link.LinkText, link.ActionName, controllerName, null, new { @class = "current" }));
            actualCssClass = "afterCurrent";
        }
        else
        {
            menuElements.Add(helper.ActionLink(link.LinkText, link.ActionName, controllerName, null, new { @class = actualCssClass }));
        }
    }

    return menuElements;
}

and in view:

@{
    List<MvcHtmlString> actionMenu = Html.SubMenuLinks("Manager", new List<Link>()
    {
        new Link() { LinkText = "linkText", ActionName = "actionName" },
        new Link() { LinkText = "linkText2", ActionName = "actionName2" }
    });
}
@section SideMenu
{
    @for (int i = 0; i < actionMenu.Count; i++) 
    { 
        <li id="menu@(i)">@actionMenu.ElementAt(i)</li>
    }
}

Not perfect, but it works at least.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文