Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add html5 data attribute to Html.TextBox in asp.net mvc?

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.

like image 343
cameron Avatar asked Mar 31 '13 08:03

cameron


2 Answers

Try using underscore _ character.

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

The runtime will convert that to data-myid

like image 134
scartag Avatar answered Sep 28 '22 04:09

scartag


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

@Html.BuildTextBox("date", Model.Date).Data("myid", "aaa")
like image 28
Jeremy Bell Avatar answered Sep 28 '22 02:09

Jeremy Bell