2

Hi I need to do the following:

@Html.TextBoxFor(m => m.Login, 
                 new { 
                       @class = "form-control", 
                       @placeholder = "Username", 
                       required="true" data-required-message="Please insert your name" 
                     })

But Im getting error on data-required-message seems that I can't use "-".

Any clue?

1

2 Answers 2

7

You have to use it with underscore _ and razor will render it as - in html:

data_required_message="Please insert your name"

another thing to note is that you don't need to put @ sign in front of every parameter of htmlAttributes , we put it for class becasue class is a reserved word in c#, so it cannot be used directly as a variable name.

your final code will be like:

@Html.TextBoxFor(m => m.Login, 
                 htmlAttributes: new { 
                                       @class = "form-control",
                                       placeholder = "Username",
                                       required="true",
                                       data_required_message="Please insert your name" 
                                     })

you are also missing a comma after placeholder attribute.

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

Comments

0

Try data_required_message instead of data-required-message

Comments

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.