Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem setting an html attribute containing hyphens in ASP.NET MVC

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.

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-').

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).

Thanks in advance.

like image 389
Matthijs Wessels Avatar asked Feb 28 '11 08:02

Matthijs Wessels


1 Answers

Try using underscores which should be automatically converted into dashes by the standard HTML helpers:

new { data_something_something = "value" }
like image 113
Darin Dimitrov Avatar answered Oct 11 '22 14:10

Darin Dimitrov