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

2019-01-09 05:42发布

I have two @Html.ActionLink's that I want to make look like buttons. I'm able to make this happen with CSS but 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("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.

3条回答
我只想做你的唯一
2楼-- · 2019-01-09 06:32

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

查看更多
3楼-- · 2019-01-09 06:39

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:

 @Html.ActionLink("Print PO", "PoReport", new { id = 51970}, new { id = "PoPrint", @class = "PoClass"})
查看更多
甜甜的少女心
4楼-- · 2019-01-09 06:45

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

@Html.ActionLink("Link Text", "ActionName", 
         new { controller = "MyController", id = 1 }, 
         new { @class = "my-class" })
查看更多
登录 后发表回答