如何在HTML属性名称ASP.NET MVC razor视图引擎上使用自定义符号 [英] How to use custom symbols on HTML attribute name ASP.NET MVC razor view engine

查看:93
本文介绍了如何在HTML属性名称ASP.NET MVC razor视图引擎上使用自定义符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Asp.Net MVC 5 Razor View Engine html属性名称上使用自定义符号。



For Instance:

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" }`





我想在属性名称上使用下面的符号。我想要转义@ symbol.It应该喜欢这个



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

@@change="bla bla"





.

@,:,.





我怎么能做这个Asp。 Net MVC 5 Razor View引擎?



谢谢



我尝试过:



new {style =width:325px;,@ class =form-control,@ v_on.change =selectedCity}



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

推荐答案

记住新{property =value};是创建匿名类型的语法。因此,类型中定义的属性名称仍必须遵循标识符命名规则。所以禁止使用诸如'@',':','。'之类的符号。



在标识符之前使用@并不是逃避,但是C#语言的一个功能是允许你使用关键字作为标识符。



因此,如果你想使用高度自定义的属性名称,你唯一的选择就是传递一个IDictionary 。说到这一点,我鼓励您改为使用自定义属性的数据前缀。不仅是标准,而且HtmlHelper还有内置支持。在定义匿名类型时,只需使用下划线代替连字符,HtmlHelper将转换它们。
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.


这篇关于如何在HTML属性名称ASP.NET MVC razor视图引擎上使用自定义符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆