How do I apply a CSS class to Html.ActionLink in ASP.NET MVC?

前端 未结 7 625
借酒劲吻你
借酒劲吻你 2020-12-01 06:02

I\'m building an ASP.NET MVC application, using VB.NET and I\'m trying to apply a css class to a Html.ActionLink using the code:



        
相关标签:
7条回答
  • 2020-12-01 06:17

    This syntax worked for me in MVC 3 with Razor:

    @Html.ActionLink("Delete", "DeleteList", "List", new { ID = item.ID, ListID = item.id }, new {@class= "delete"})
    
    0 讨论(0)
  • 2020-12-01 06:18

    In C# it also works with a null as the 4th parameter.

    @Html.ActionLink( "Front Page", "Index", "Home", null, new { @class = "MenuButtons" })
    
    0 讨论(0)
  • 2020-12-01 06:22

    In VB.NET

    <%=Html.ActionLink("Contact Us", "ContactUs", "Home", Nothing, New With {.class = "link"})%>
    

    This will assign css class "link" to the Contact Us.

    This will generate following HTML :

    <a class="link" href="www.domain.com/Home/ContactUs">Contact Us</a>
    
    0 讨论(0)
  • 2020-12-01 06:23

    @ewomack has a great answer for C#, unless you don't need extra object values. In my case, I ended up using something similar to:

    @Html.ActionLink("Delete", "DeleteList", "List", new object { },
    new { @class = "delete"})
    
    0 讨论(0)
  • 2020-12-01 06:25

    It is:

    <%=Html.ActionLink("Home", "Index", MyRouteValObj, new with {.class = "tab" })%>
    

    In VB.net you set an anonymous type using

    new with {.class = "tab" }
    

    and, as other point out, your third parameter should be an object (could be an anonymous type, also).

    0 讨论(0)
  • 2020-12-01 06:28

    This works for MVC 5

    @Html.ActionLink("LinkText", "ActionName", new { id = item.id }, new { @class = "btn btn-success" })
    
    0 讨论(0)
提交回复
热议问题