113

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.

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
LiamGu
  • 5,317
  • 12
  • 49
  • 68

7 Answers7

160

@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"})
Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
adamgede
  • 2,112
  • 2
  • 15
  • 9
  • 25
    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"})` – xec Aug 04 '14 at 10:18
60

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

@Html.ActionLink( "Front Page", "Index", "Home", null, new { @class = "MenuButtons" })
Liam
  • 27,717
  • 28
  • 128
  • 190
coding_is_fun
  • 1,021
  • 10
  • 5
49

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

Eduardo Molteni
  • 38,786
  • 23
  • 141
  • 206
35

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"})
ewomack
  • 753
  • 13
  • 29
21

This works for MVC 5

@Html.ActionLink("LinkText", "ActionName", new { id = item.id }, new { @class = "btn btn-success" })
César León
  • 2,941
  • 1
  • 21
  • 18
  • 1
    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" } ) – mggSoft Jun 12 '18 at 10:47
  • @mggSoft YES!! This worked for me using MVC 5. Thank you – Clancinio Aug 21 '20 at 17:29
3

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>
H Sampat
  • 1,039
  • 1
  • 9
  • 10
1

This will work , tested in MVC core 6

  • Good code readability

  • it allow you set other html attributes , eg i use target attribute to open new tab

  • it allow you set class value , eg i use bootstrap convert hyperlink to button

  • it allow you apply custom inline style

         @Html.ActionLink(
                 linkText: "Button\Hyperlink Label",
                 actionName: "Index",
                 controllerName: "Home",
                 routeValues: new { Id = @item.ToString() },
                 htmlAttributes: new { target = "_blank",@class="btn" , @style="background-color:lightblue;margin: 10px 30px;"})
    
HO LI Pin
  • 1,441
  • 13
  • 13