VAAA
VAAA

Reputation: 15039

Razor input element custom attribute

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?

Upvotes: 2

Views: 4173

Answers (2)

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

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.

Upvotes: 7

Try data_required_message instead of data-required-message

Upvotes: 0

Related Questions