Use 'class' (or other reserved keyword) as property on anonymous type
Asked Answered
T

2

9

Ok, I cant find the answer to this:

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

I want to set the CSS class attribute of the generated element.

Obviously, C# will not allow me to use "class" as the name of an object's member.

What should I do?

Titograd answered 22/9, 2010 at 5:11 Comment(0)
L
18

Can You try with escaping the class with : @.

So, please modify your code to :

<%: Html.ActionLink("Click Here", "Action", null, new {@class="myClass"})%>
Lange answered 22/9, 2010 at 5:14 Comment(0)
V
0

Using a capital 'C' in Class also works, from this answer to a similar question. The attribute names are converted to lower case.

<%: Html.ActionLink("Click Here", "Action", null, new { Class = "myClass" })%>
Vorticella answered 11/4, 2019 at 0:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.