问题设置包含在ASP.NET MVC中的连字符的HTML属性 [英] Problem setting an html attribute containing hyphens in ASP.NET MVC
问题描述
我已经定义了一个自定义HTML属性数据几岁出头。在我看来,我使用HTML扩展方法来创建实例的文本框。其中一个参数是一个匿名对象HtmlAttributes
。我想通过这个值:新{数据几岁出头=值}
。然而,数据的东西,东西
不被.NET承认,因为连字符的属性名称。
I have defined a custom html attribute "data-something-something". In my view I use an Html extension method to create for instance a text box. One of the parameters is an anonymous object HtmlAttributes
. I want to pass this value: new { data-something-something = "value" }
. However, data-something-something
is not recognized by .NET as a property name because of the hyphens.
我把它改为 dataSomethingSomething
现在,但我想根据HTML 5标准(即preFIX用数据 - 来定义我的自定义属性')。
I changed it to dataSomethingSomething
for now, but I would like to define my custom attribute according to the HTML 5 standard (i.e. prefix it with 'data-').
我不得不尝试做新{类=类名}
的类属性时,前一个类似的问题。对于这种情况,我发现我可以preFIX 类
用@符号,使其工作(即新{@class =类名}
)。因为对这种情况下的解决方案,我希望有可能是我当前的问题($ P $用@没有工作pfixing它)。一个解决方案
I had a similar problem before when trying to do new { class = "class-name"}
on the class property. For this case I found that I can prefix class
with an '@' symbol to make it work (i.e. new { @class = "class-name"}
). Because there is a solution for this scenario, I hoped that there might be a solution for my current problem (prefixing it with an '@' did not work).
在此先感谢。
推荐答案
尝试使用下划线它应该由标准的HTML佣工被自动转换成破折号:
Try using underscores which should be automatically converted into dashes by the standard HTML helpers:
new { data_something_something = "value" }
这篇关于问题设置包含在ASP.NET MVC中的连字符的HTML属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!