Solutions for Using HTML5 Data-* Attributes in ASP.NET MVC

Dec 07, 2025 · Programming · 11 views · 7.8

Keywords: ASP.NET MVC | HTML5 | Data Attributes | C# | JavaScript

Abstract: This article explores how to correctly use HTML5 data-* custom data attributes in ASP.NET MVC projects. It addresses the issue where C# anonymous types do not support hyphenated property names and provides multiple solutions, including using dictionaries, custom types, and leveraging built-in support in ASP.NET MVC 3+. Code examples are provided for each method, along with a comparison of their pros and cons to help developers choose the most suitable approach.

Introduction

HTML5 introduces data-* custom data attributes, allowing developers to store additional information in HTML elements for access and processing via JavaScript. In ASP.NET MVC, when using HTML helpers like Html.ActionLink, it is often necessary to add these attributes to enhance front-end interaction.

Problem Description

In ASP.NET MVC HTML helpers, attributes are typically specified using anonymous objects. However, C# anonymous type property names cannot contain hyphens, such as "data-details", because hyphens are not valid identifier characters. Attempting to use them results in compilation error CS0746.

Solutions

Method 1: Using a Dictionary Instead of an Anonymous Object

A simple approach is to use a Dictionary<string, Object> to specify HTML attributes. This allows the use of hyphens as keys.

<%= Html.ActionLink("back", "Search", new { keyword = Model.Keyword, page = Model.currPage - 1}, new Dictionary<string,Object> { {"class","prev"}, {"data-details","Some Details"} })%>

Method 2: Creating a Custom Type

For a more structured approach, define a custom class and use the DisplayName attribute to specify HTML attribute names.

public class CustomHtmlAttributes { [DisplayName("class")] public string Class { get; set; } [DisplayName("data-details")] public string DataDetails { get; set; } } <%= Html.ActionLink("back", "Search", new { keyword = Model.Keyword, page = Model.currPage - 1}, new CustomHtmlAttributes { Class = "prev", DataDetails = "Some Details" })%>

Method 3: Leveraging Built-in Support in ASP.NET MVC 3+

Starting from ASP.NET MVC 3, the framework automatically converts underscores in anonymous object property names to hyphens. Therefore, you can use underscores like "data_details", which will be rendered as "data-details".

@Html.TextBoxFor(vm => vm.City, new { data_bind = "foo" })

Renders as: <input data-bind="foo" id="City" name="City" type="text" value="" />

For older versions, you can mimic MVC 3's behavior by creating a helper method.

public static RouteValueDictionary AnonymousObjectToHtmlAttributes(object htmlAttributes) { RouteValueDictionary result = new RouteValueDictionary(); if (htmlAttributes != null) { foreach (var property in TypeDescriptor.GetProperties(htmlAttributes)) { result.Add(property.Name.Replace('_', '-'), property.GetValue(htmlAttributes)); } } return result; }

Then use: <%: Html.TextBoxFor(vm => vm.City, AnonymousObjectToHtmlAttributes(new { data_bind = "foo" })) %>

Conclusion

When using HTML5 data-* attributes in ASP.NET MVC, choose the appropriate method based on the project version and requirements. For older versions, it is recommended to use dictionaries or custom types; for MVC 3 and above, underscores can be used directly. These methods ensure compatibility with HTML5 specifications and facilitate JavaScript processing.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.