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

2

15

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!

Voucher answered 3/3, 2011 at 18:35 Comment(1)
linked question is VB.NET, this is C#, so not quite an exact duplicateAnaesthesiology
F
19

Make sure you are using the proper overload:

<%: Html.ActionLink("Home", "Index", "Home", null, new { @class = "NavLink" })%>
                                              ^                ^
                                          routeValues    htmlAttributes
Fractionize answered 3/3, 2011 at 18:38 Comment(0)
M
1

Method Actionlink have some overloading.

If you want to determine some html attributes, you should use such methods(in your case):

  ActionLink(HtmlHelper, String, String, RouteValueDictionary, IDictionary<String, Object>)
  ActionLink(HtmlHelper, String, String, String, Object, Object)
  ActionLink(HtmlHelper, String, String, String, RouteValueDictionary, IDictionary<String, Object>)

More about this here: http://msdn.microsoft.com/en-us/library/system.web.mvc.html.linkextensions.actionlink.aspx

Mochun answered 3/3, 2011 at 18:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.