Attention! This is a translation of the ASP.NET MVC issue. Adding Attribute “data-message” to Html.TextBoxFor

It is necessary to display an additional data attribute in the form element:

<input name="file" type="file" class="file" data-show-preview="false"> 

How to do this with a strongly typed helper? The following option does not work:

 @Html.TextBoxFor(x => x.File, new { type = "file", data-show-preview = "false", @class = "file" }) 

I get an error:

Invalid anonymous type member declarator. Anonymous type members must be declared a member

1 answer 1

Use the underscore character instead of the hyphen in the data attribute name:

 @Html.TextBoxFor(x => x.File, new { type = "file", data_show_preview = "false", @class = "file" }) 

The TextBoxFor helper knows what to do and will replace the underscore characters back into hyphens when generating the markup.