31

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
  • 57,247
  • 24
  • 95
  • 149
Ian Vink
  • 63,888
  • 100
  • 326
  • 544
  • 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

80

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
  • 994,864
  • 265
  • 3,241
  • 2,902
  • 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