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

无人久伴 提交于 2019-11-29 17:19:30

问题


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!


回答1:


Make sure you are using the proper overload:

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



回答2:


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



来源:https://stackoverflow.com/questions/5184976/how-to-use-css-on-an-html-actionlink-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!