How to use CSS on an Html.ActionLink in C#

pongahead picture pongahead · Mar 3, 2011 · Viewed 8.1k times · Source

I tried this code

<%: Html.ActionLink("Home", "Index", "Home", new { @class = "NavLink" })%>

and it links to the css so that I can style the link, but it changes the link to have a different URL that is not to my controller like it is without the new { @class = "NavLink" }. Is there any way to let me style these links without ruining my URLs so they go to the correct pages?

Thanks!

Answer

Darin Dimitrov picture Darin Dimitrov · Mar 3, 2011

Make sure you are using the proper overload:

<%: Html.ActionLink("Home", "Index", "Home", null, new { @class = "NavLink" })%>
                                              ^                ^
                                          routeValues    htmlAttributes