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.

1

1 Answer 1

85

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @Darin and shame on MVC to not handling this little things!
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.