【问题标题】:How do I add dynamic htmlAttributes to htmlhelper ActionLinks?如何将动态 htmlAttributes 添加到 htmlhelper ActionLinks?
【发布时间】:2010-03-20 16:05:26
【问题描述】:

在我的母版页中,我有一个使用 ActionLinks 创建的顶级菜单:

<ul id="topNav">
  <li><%=Html.ActionLink("Home", "Index", "Home")%></li>
  <li><%=Html.ActionLink("News", "Index", "News")%></li>
  <li><%=Html.ActionLink("Projects", "Index", "Projects")%></li>
  <li><%=Html.ActionLink("About", "About", "Home")%></li>
  <li><%=Html.ActionLink("Contact", "Contact", "Home")%></li>
  <li><%=Html.ActionLink("Photos", "Photos", "Photos")%></li>
</ul> 

我想动态添加一个名为“current”的类到站点当前指向的链接。因此,例如,当网站位于主页时,菜单链接将呈现如下:

<li><a class="current" href="/">Home</a></li>

我是否必须重载 ActionLink 方法才能做到这一点,或者创建一个全新的 HtmlHelper,还是有更好的方法?

我对 MVC 还很陌生,所以我不确定解决这个问题的正确方法是什么。

提前致谢。

【问题讨论】:

    标签: asp.net-mvc vb.net html-helper


    【解决方案1】:

    您可以为此编写自己的扩展方法(对不起,我的 VB 有点生疏):

    <Extension> _
    Public Shared Function MyActionLink( _
        ByVal htmlHelper As HtmlHelper, _ 
        ByVal linkText As String, _ 
        ByVal actionName As String, _ 
        ByVal controllerName As String) As MvcHtmlString
    
        Dim currentAction As String = TryCast(htmlHelper.ViewContext.RouteData.Values.Item("action"), String)
        Dim currentController As String = TryCast(htmlHelper.ViewContext.RouteData.Values.Item("controller"), String)
        If ((actionName = currentAction) AndAlso _
            (controllerName = currentController)) _ 
        Then
            Return htmlHelper.ActionLink( _
                linkText, _
                actionName, _
                controllerName, _ 
                Nothing, _
                New { _
                    .class = "current" _
                })
        End If
        Return htmlHelper.ActionLink(linkText, actionName, controllerName)
    End Function
    

    你可以这样使用:

    <ul id="topNav">
      <li><%=Html.MyActionLink("Home", "Index", "Home")%></li>
      <li><%=Html.MyActionLink("News", "Index", "News")%></li>
      <li><%=Html.MyActionLink("Projects", "Index", "Projects")%></li>
      <li><%=Html.MyActionLink("About", "About", "Home")%></li>
      <li><%=Html.MyActionLink("Contact", "Contact", "Home")%></li>
      <li><%=Html.MyActionLink("Photos", "Photos", "Photos")%></li>
    </ul> 
    

    【讨论】:

    • 这很完美!我只需对您的代码示例进行一项修改:htmlAttributes 对象的语法是 New With { .class="current" } 谢谢!
    • 很高兴你完成了这项工作。正如我所说,我的 VB.NET 生锈了 :-)
    • +1,比我的解决方案好得多,当我得到它时,我将用你的方法替换它。
    【解决方案2】:

    从它的声音你可能想要这样的东西。

    为每个控制器创建一个默认构造函数,并在 ViewData 中设置一个值。

    public HomeController()
        : base()
    {
        ViewData["selected"] = "home";
    }
    

    然后在您的 Site.Master 文件中,您可以这样做:

    <li class="<%= ((ViewData["selected"] == "home") ? "current" : "") %>"><%= Html.ActionLink("Home", "Index", "Home") %></li>
    

    可以稍微清理一下,但你明白了。

    【讨论】:

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