代码之家  ›  专栏  ›  技术社区  ›  Mike

如何在网络中实现“如果活动”检测Html.Helper?

  •  0
  • Mike  · 技术社区  · 15 年前

    我不确定这是一个简单的误解,还是不正确的范例来使用这种功能,但这是我到目前为止想到的。。。

    可以!所以。代码在哪里?

    我已经实现了下面的Html助手扩展,以帮助从我需要的标准链接创建稍微“调整”的链接。

    public static MvcHtmlString ActiveActionLink (this HtmlHelper helper, string labelText, string action, string controller)
    {
     var cssProprties = controller + " active";
     return helper.ActionLink(labelText, action, controller, null, new { @class = cssProprties });
    }
    

    我的理论是,我所需要做的就是挖掘正确的对象来定位我当前所在的控制器,并将其与传入的“controller”参数匹配。如果这些匹配,则添加活动类。

    所以,有两个问题。。。

    如果是这样的话:我可以从我所处的层次挖掘哪些对象来获取我所需要的信息进行比较?


    更新:

    当我写一个问题的时候,通常发生的事情发生了,所以,我有了一个脑电波。我注意到BeginForm帮助程序中的一块代码导致我创建了以下示例:

    public static MvcHtmlString ActiveActionLink (this HtmlHelper helper, string labelText, string action, string controller)
    {
        var cssProprties = controller;
    
        // if this controller is the target controller, page is active.
        if (helper.ViewContext.RouteData.Values["controller"].ToString() == controller)
            cssProprties += " active";
    
        return helper.ActionLink(labelText, action, controller, null, new { @class = cssProprties });
    }
    

    所以我猜这回答了“我可以挖哪些物体?”问题。同时,这也直接受到MVC源代码的影响,大概是“这是正确的/可以接受的”思维方式吗ASP.NETmvc?“还有问题吗?

    2 回复  |  直到 15 年前
        1
  •  0
  •   Chao    15 年前

    这似乎是一种正确的方法,尽管你需要考虑一些事情。

        2
  •  0
  •   Mike    15 年前

    这就是我决定要解决的问题。它解决了我遇到的问题&直接受源代码的影响asp.netmvc2型。我希望它也能帮助别人。

    public static MvcHtmlString ActiveActionLink (this HtmlHelper helper, string labelText, string action, string controller)
    {
        var cssProprties = controller;
    
        // if this controller is the target controller, page is active.
        if (helper.ViewContext.RouteData.Values["controller"].ToString() == controller)
            cssProprties += " active";
    
        return helper.ActionLink(labelText, action, controller, null, new { @class = cssProprties });
    }