How do I add dynamic htmlAttributes to htmlhelper ActionLinks?

Posted by camainc on Stack Overflow See other posts from Stack Overflow or by camainc
Published on 2010-03-20T16:05:26Z Indexed on 2010/03/20 16:11 UTC
Read the original article Hit count: 360

Filed under:
|
|

In my master page I have a top-level menu that is created using 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> 

I want to dynamically add a class named "current" to the link that the site is currently pointing to. So, for example, when the site is sitting at the home page, the menu link would render like this:

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

Do I have to overload the ActionLink method to do this, or create an entirely new HtmlHelper, or is there a better way?

I'm fairly new to MVC, so I'm not sure what is the correct way to go about this.

Thanks in advance.

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about vb.net