33

Using MVC 4 I create a text box for a model property with the "data-message" attribute:

@Html.TextBoxFor(o => o.TradeOrder.Symbol, new {data-message="Required"})

However, I get the following error:

Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.

tereško
  • 58,060
  • 25
  • 98
  • 150
Ian Vink
  • 66,960
  • 104
  • 341
  • 555
  • here is a link with more information http://stackoverflow.com/questions/2520487/how-to-use-html-5-data-attributes-in-asp-net-mvc – Matt Bodily Oct 25 '13 at 17:44

1 Answers1

84

Use _:

@Html.TextBoxFor(o => o.TradeOrder.Symbol, new {data_message="Required"})

The TextBoxFor helper will know what to do and replace it with - when generating the markup.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Thanks @Darin and shame on MVC to not handling this little things! – Amir978 Jun 27 '14 at 04:33
  • Although correct, one thing I found is that the case is not preserved. data_maxDate renders as data-maxdate. I haven't figured out how to fix this. I want to use data attributes to pass as properties off to a JQuery plugin so the case must match. – Jason Butera Jun 17 '21 at 16:00