Can't use data attribute that ends in "name"?

asp.net-mvc, asp.net-mvc-4, razor

Solution

Thanks to Tommy for testing the behavior I described and finding it wasn't a bug for everyone.

After Tommy wrote this, I looked at the Helper we were using. I realized that we were actually using an Extension method called "NameLessTextBoxFor" (which we found here: How to extend html.textboxfor to remove the name attribute?), which removes the `name=""` attribute from the input before displaying it. I should have confirmed this before posting, but didn't recognize it could affect the HTML attributes passed into it.

And lo and behold, as you would probably expect, the functionality of this method was also cutting off any attribute that contained `name=""`. It was doing a very simple search and replace on that text and removing it. So that was the issue here.

Thanks for your time and attention to this issue and apologize I didn't catch this myself.

Problem

I am well aware that you use underscores for data attributes with hyphens ("data_bind" instead of "data-bind", in the object), and they automatically get replaced with hyphens. But I have run into the problem where you can't do this underscore "hack," if the attribute ends with "name". So I have tried both of these, but neither work: ``` @Html.TextBoxFor(model => model.Street, new { data_encrypted_name = "street" }) @Html.TextBoxFor(model => model.Street, new { @data_encrypted_name = "street" }) ``` When I view the HTML that is generated, for both cases above, it generates: ``` <input data-encrypted- id="ViewModel_Street" name="ViewModel.Street" type="text" value="" /> ``` At first, I thought this might have something to do with multiple underscores/hyphens, but I tried two more test cases, to see what would happen, and they both worked just fine: ``` @Html.TextBoxFor(model => model.Street, new { data_encrypted_namme = "street" }) @Html.TextBoxFor(model => model.Street, new { data_name_encrypted = "street" }) ``` So this problem is definitely related to having "name" at the end of the attribute. Am I doing something wrong or missing something, or is this a bug in how .NET converts the attributes? (For clarification, we use Braintree Payments, and they require the use of the "data-encrypted-name" attribute on certain inputs, so we can't just choose another attribute name.)

Original source