How do I apply a CSS class to Html.ActionLink in A

2019-01-08 15:47发布

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.

7条回答
迷人小祖宗
2楼-- · 2019-01-08 16:21

It is:

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

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

查看更多
Explosion°爆炸
3楼-- · 2019-01-08 16:27

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"})
查看更多
该账号已被封号
4楼-- · 2019-01-08 16:33

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>
查看更多
▲ chillily
5楼-- · 2019-01-08 16:46

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

@Html.ActionLink( "Front Page", "Index", "Home", null, new { @class = "MenuButtons" })
查看更多
看我几分像从前
6楼-- · 2019-01-08 16:46

This works for MVC 5

@Html.ActionLink("LinkText", "ActionName", new { id = item.id }, new { @class = "btn btn-success" })
查看更多
趁早两清
7楼-- · 2019-01-08 16:46

deleted the c#... here is the vb.net

<%=Html.ActionLink("Home", "Index", "Home", New With {.class = "tab"}, Nothing)%>
查看更多
登录 后发表回答