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?

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
VAAA
  • 14,531
  • 28
  • 130
  • 253
  • 1
    Possible duplicate of [How to specify data attributes in razor, e.g., data-externalid="23151" on @this.Html.CheckBoxFor(...)](http://stackoverflow.com/questions/9444805/how-to-specify-data-attributes-in-razor-e-g-data-externalid-23151-on-this) – Jasen Dec 29 '16 at 19:57

2 Answers2

6

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.

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
0

Try data_required_message instead of data-required-message