Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 336
DanC Avatar asked Sep 22 '10 05:09

DanC


People also ask

What keyword do you use to declare an anonymous type?

Essentially an anonymous type is a reference type and can be defined using the var keyword. You can have one or more properties in an anonymous type but all of them are read-only.

What is anonymous data type in C#?

Anonymous types in C# are the types which do not have a name or you can say the creation of new types without defining them. It is introduced in C# 3.0. It is a temporary data type which is inferred based on the data that you insert in an object initializer.

What is the difference between an anonymous type and a regular data type?

From the perspective of the common language runtime, an anonymous type is no different from any other reference type, except that it cannot be cast to any type except for object.

How anonymous classes instantiated in .NET can they be passed as function params?

We can't use a anonymous typed variable as a parameter for a method. But we can pass anonymous type for a method that accept dynamic type. Example: public class Employee.


1 Answers

Can You try with escaping the class with : @.

So, please modify your code to :

<%: Html.ActionLink("Click Here", "Action", null, new {@class="myClass"})%>
like image 126
Siva Gopal Avatar answered Sep 30 '22 16:09

Siva Gopal