DanC
DanC

Reputation: 8805

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

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?

Upvotes: 9

Views: 2101

Answers (2)

kristianp
kristianp

Reputation: 5895

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" })%>

Upvotes: 0

Siva Gopal
Siva Gopal

Reputation: 3502

Can You try with escaping the class with : @.

So, please modify your code to :

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

Upvotes: 18

Related Questions