Click here to Skip to main content
15,887,945 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to use custom symbols on Asp.Net MVC 5 Razor View Engine html attribute name.

For Instance:
`new { style = "width: 325px;", @class = "form-control",v_on:change="selectedCity" }`


I want to use below symbols on attribute name.I want to escape @ symbol.It should like this
@@change="bla bla"
.

@,:,.


How can I do this Asp.Net MVC 5 Razor View Engine?

Thanks

What I have tried:

new { style = "width: 325px;", @class = "form-control",@v_on.change="selectedCity" }
Posted
Updated 6-Jun-16 3:58am
Comments
John C Rayan 6-Jun-16 7:21am    
What are you trying to do? what is v_on.change ?

1 solution

Remember that new { property = "value" }; is the syntax for creating an anonymous type. As such, the property names defined within the type still have to follow the identifier naming rules. So using symbols such as '@', ':', '.' is prohibited.

Using the @ before an identifier isn't so much as an escape, but a feature of the C# language that lets you use keywords as identifiers.

So if you want to use highly customized attribute names, your only option is to pass an IDictionary. With saying that, I encourage you to to instead use the data- prefix for custom attributes. Not only is that standard, but the HtmlHelper has built-in support. Simply use underscores in place of the hyphens when defining your anonymous type, and the HtmlHelper will convert them.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900