25

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

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

这是我正在应用的样式:

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

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

4

5 回答 5

77

您必须使用该@字符,因为 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" })
于 2013-09-05T18:34:51.220 回答
6

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

 @Html.ActionLink("Print PO", "PoReport", new { id = 51970}, new { id = "PoPrint", @class = "PoClass"})
于 2013-09-05T18:32:00.493 回答
3

您必须指明动作名称、控制器和 url 参数(本例中为空)

@Html.ActionLink("Link Name", 
"ActionName",
"ControllerName",
null,
new { @class = "your css class" }
)
于 2019-02-12T16:29:44.057 回答
1

执行此new { @class = "PoClass"} 操作您需要 @ 来表示 class 等关键字

于 2013-09-05T18:32:11.703 回答
0

当我使用 null 时它起作用了

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

“添加用户”是按钮文本

“创建”是 actionName

无效的

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

于 2020-04-16T05:38:48.477 回答