如何将 CSS 类应用于 ASP.NET MVC 中的 Html.ActionLink?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1444495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 21:05:09  来源:igfitidea点击:

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

cssasp.net-mvcvb.nethtml-helper

提问by LiamGu

I'm building an ASP.NET MVCapplication, using VB.NETand I'm trying to apply a css class to a Html.ActionLinkusing the code:

我正在使用VB.NET构建一个ASP.NET MVC应用程序,并且我正在尝试使用以下代码将 css 类应用于 a :Html.ActionLink

<%=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.

编译器错误消息:BC30988:应为类型或“With”。

I'm new to MVCand 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.

我是MVC 的新手,真的不知道我在做什么,所以我看不出那里有什么问题,因为我在其他地方使用基于示例的代码。

采纳答案by Eduardo Molteni

It is:

这是:

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

In VB.net you set an anonymous type using

在 VB.net 中,您使用以下方法设置匿名类型

new with {.class = "tab" }

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

并且,正如其他人指出的那样,您的第三个参数应该是一个对象(也可以是匿名类型)。

回答by adamgede

@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:

@ewomack 对 C# 有很好的回答,除非您不需要额外的对象值。就我而言,我最终使用了类似的东西:

@Html.ActionLink("Delete", "DeleteList", "List", new object { },
new { @class = "delete"})

回答by coding_is_fun

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

在 C# 中,它也可以使用 null 作为第四个参数。

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

回答by ewomack

This syntax worked for me in MVC 3 with Razor:

这个语法在带有 Razor 的 MVC 3 中对我有用:

@Html.ActionLink("Delete", "DeleteList", "List", new { ID = item.ID, ListID = item.id }, new {@class= "delete"})

回答by César León

This works for MVC 5

这适用于 MVC 5

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

回答by H Sampat

In VB.NET

在 VB.NET 中

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

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

这会将 css 类“链接”分配给联系我们。

This will generate following HTML :

这将生成以下 HTML :

<a class="link" href="www.domain.com/Home/ContactUs">Contact Us</a>

回答by rajesh pillai

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

删除了 c#... 这里是 vb.net

<%=Html.ActionLink("Home", "Index", "Home", New With {.class = "tab"}, Nothing)%>