14

The Html.TextBox syntax is:

public static MvcHtmlString TextBox(this HtmlHelper htmlHelper, string name, 
       object value, object htmlAttributes);

So, i put my data attribute in htmlAttributes. I have tried

@Html.TextBox("date",Model.Date,new { data-myid="aaa"})

but that doesn't work for me.

Liam
  • 27,717
  • 28
  • 128
  • 190
cameron
  • 2,976
  • 3
  • 23
  • 35

2 Answers2

28

Try using underscore _ character.

@Html.TextBox("date",Model.Date,new { data_myid="aaa"})

The runtime will convert that to data-myid

scartag
  • 17,548
  • 3
  • 48
  • 52
1

For a friendlier syntax, you could try: http://buildmvc.codeplex.com/

@Html.BuildTextBox("date", Model.Date).Data("myid", "aaa")
Jeremy Bell
  • 698
  • 5
  • 9