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

197,372

Solution 1

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).

Solution 2

@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"})

Solution 3

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

@Html.ActionLink( "Front Page", "Index", "Home", null, new { @class = "MenuButtons" })

Solution 4

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"})

Solution 5

This works for MVC 5

@Html.ActionLink("LinkText", "ActionName", new { id = item.id }, new { @class = "btn btn-success" })
Share:
197,372

Related videos on Youtube

LiamGu
Author by

LiamGu

I make software delivery faster.

Updated on November 27, 2020

Comments

  • LiamGu
    LiamGu over 3 years

    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:

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

    But when I run the code I receive the below error:

    Compiler Error Message: BC30988: Type or 'With' expected.

    I'm new to MVC and really haven't much of a clue what I'm doing so I can't see what's wrong there as I'm using code based of an example elsewhere.

    • Jason Lee
      Jason Lee almost 15 years
      There is no such a signature for Html.ActionLink method with (string, string, string, object).
    • 3Dave
      3Dave over 14 years
      Is there anyway to do this without using an anonymous class?
  • xec
    xec almost 10 years
    If you don't need the route values you can also pass null as the 4th argument: @Html.ActionLink("Delete", "DeleteList", "List", null, new { @class = "delete"})
  • mggSoft
    mggSoft about 6 years
    For using in a MVC 5 ActionLink with parameters: @Html.ActionLink("Text of the link", "Action", "Controller name", new { myParam = "XXX" }, new { @style = "color:black" } )
  • Sumanstm21
    Sumanstm21 over 5 years
    This is Helpful because when you add null, you are getting a standard and clean url request
  • Clancinio
    Clancinio almost 4 years
    @mggSoft YES!! This worked for me using MVC 5. Thank you