asp.net-mvc 如何在 Razor 中的 ActionLinks 上指定 css 类名?

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

How can you specify a css class name on ActionLinks in Razor?

asp.net-mvcrazor

提问by jgauffin

The following code generates an error:

以下代码会产生错误:

@Html.ActionLink("Title", "action", new { id=1 }, new { @class = "myCssClass" });

I tried to use @ since classis a keyword. How should I write it when using razor?

我尝试使用 @ 因为它class是一个关键字。用剃刀的时候应该怎么写?

Edit

编辑

The problem was not really the at sign, but that I didn't use blocks together with my if:

问题并不是真正的 at 符号,而是我没有将块与我的if:一起使用:

@if (blabla)
    @Html.ActionLink("Title", "action", new { id=1 }, new { @class = "myCssClass" });

Works:

作品:

@if (blabla)
{
    @Html.ActionLink("Title", "action", new { id=1 }, new { @class = "myCssClass" });
}

Up voted both answers since they made me realize the problem.

Up 对这两个答案都投了赞成票,因为它们让我意识到了这个问题。

回答by Aleksei Anufriev

Try to write something like:

尝试写一些类似的东西:

@(Html.ActionLink("Title", "action", new { id=1 }, new { @class = "myCssClass" }));

There is a good post about Razor related to your problem: ScottGu Blog

有一篇关于 Razor 的好文章与您的问题相关: ScottGu 博客

回答by Darin Dimitrov

Simply:

简单地:

@Html.ActionLink("Title", "action", new { id=1 }, new { @class = "myCssClass" })

will work in ASP.NET MVC 3 RC2. Razor is intelligent.

将在 ASP.NET MVC 3 RC2 中工作。剃须刀很聪明。