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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用'class'(或其他保留关键字)作为匿名类型的属性相关的知识,希望对你有一定的参考价值。

好的,我找不到答案:

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

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

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

我该怎么办?

答案

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

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

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

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

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

以上是关于使用'class'(或其他保留关键字)作为匿名类型的属性的主要内容,如果未能解决你的问题,请参考以下文章