CSS 如何将类添加到@Html.ActionLink?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18643457/
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-30 00:16:07  来源:igfitidea点击:

How do I add a class to an @Html.ActionLink?

cssasp.net-mvcrazor

提问by Alan Fisher

I have two @Html.ActionLink'sthat I want to make look like buttons. I'm able to make this happen with CSSbut only if I use the #ID of the actionlink to apply the CSS. I want to assign a class to the action links but when I do using the code below I get an error saying I have a missing "}".

我有两个@Html.ActionLink's我想让它们看起来像按钮。我能够做到这一点,CSS但前提是我使用 actionlink 的 #ID 来应用CSS. 我想为操作链接分配一个类,但是当我使用下面的代码时,我收到一条错误消息,说我缺少一个“}”。

 @Html.ActionLink("Print PO", "PoReport", new { id = 51970}, 
                 new { id = "PoPrint"} , new { class = "PoClass"})

Here is the Style I am applying:

这是我正在应用的样式:

<style>
 #PoPrint 
{
 border: 4px outset;
 padding: 2px;
 text-decoration: none;
 background-color:lightskyblue;
}
</style>

This works and I suppose I could just add the other #ID to tthe style but would like to apply the style to the Class.

这有效,我想我可以将另一个 #ID 添加到样式中,但想将样式应用于类。

回答by Jarrett Meyer

You have to use the @character, since class is a keyword in C#. Here's a link to the MSDN documentation: http://msdn.microsoft.com/en-us/library/dd492124(v=vs.108).aspx

您必须使用该@字符,因为 class 是 C# 中的关键字。这是 MSDN 文档的链接:http: //msdn.microsoft.com/en-us/library/dd492124(v= vs.108).aspx

@Html.ActionLink("Link Text", "ActionName", 
         new { controller = "MyController", id = 1 }, 
         new { @class = "my-class" })

回答by chris.house.00

The problem is that class is a reserved word in C#. You can specify that you want to use the name 'class' as your attribute name by escaping it with the @ symbol like so:

问题是 class 是 C# 中的保留字。您可以指定要使用名称“class”作为属性名称,方法是使用 @ 符号对其进行转义,如下所示:

 @Html.ActionLink("Print PO", "PoReport", new { id = 51970}, new { id = "PoPrint", @class = "PoClass"})

回答by Badr Bellaj

You have to indicate the action name, controller and url parameters (null in this example)

您必须指明操作名称、控制器和 url 参数(在此示例中为 null)

@Html.ActionLink("Link Name", 
"ActionName",
"ControllerName",
null,
new { @class = "your css class" }
)

回答by subramanya46

It worked when i use null

当我使用 null 时它起作用了

@Html.ActionLink("Add User", "Create",null, new { @class = "btn btn-primary" })

@Html.ActionLink("添加用户", "创建",null, new { @class = "btn btn-primary" })

"Add User" is Button Text

"Create" is actionName

null

new { @class = "btn btn-primary" } CSS used bootstrap class

“添加用户”是按钮文本

“创建”是操作名称

空值

new { @class = "btn btn-primary" } CSS 使用的引导类

回答by TGH

Do this new { @class = "PoClass"}You need the @ for keywords like class

这样做new { @class = "PoClass"}你需要@ 关键字像类