How to add data attributes to html element in ASP.NET MVC?

asp.net-mvc, c#

Solution

Use an underscore instead of a dash.

`new { data_myid = m.ID }`

This definitely works in MVC3 (haven't checked other versions). The underscore will be converted to a dash when the HTML is rendered.

EDIT

This works in the latest versions of MVC too.

Problem

I learned few minutes ago that adding data attributes is a nice way to add custom information to html elements. So I tried to do like this: ``` <%= Html.TextBox ("textBox", "Value", new { data-myid = m.ID })%> ``` But it ends up as a syntax error. How can I define custom data attributes? EDIT: I see that I can achieve this effect using: ``` <%= Html.TextBox ("textBox", "Value", new Dictionary<string, object> {{ "data-myid", m.ID }})%> ``` But that doesn't look...umm...clean! Is there any better way to do this?

Original source

Related problems