使用'class'(或其他保留关键字)作为匿名类型的属性

问题描述 投票:5回答:2

好的,我找不到答案:

<%: Html.ActionLink("Click Here", "Action", null, new {class="myClass"})%>

我想设置生成的元素的CSS类属性。

显然,C#不允许我使用“class”作为对象成员的名字。

我该怎么办?

asp.net-mvc-2 html-helper anonymous-types
2个回答
12
投票

你能尝试用以下方法逃避课程:@。

因此,请将您的代码修改为:

<%: Html.ActionLink("Click Here", "Action", null, new {@class="myClass"})%>

0
投票

this answer to a similar question中使用Class中的大写'C'也有效。属性名称将转换为小写。

<%: Html.ActionLink("Click Here", "Action", null, new { Class = "myClass" })%>
© www.soinside.com 2019 - 2024. All rights reserved.