Last Updated: February 25, 2016
· st3v3nhunt

Generate html attributes with dash through C# view engines

How do you generate attributes for html elements that have a dash (-) in them through a C# view template engine while using object initialiser syntax?

Use an underscore (_) instead.

An example. In a Razor view using the Html.ActionLink(...) helper to create an anchor element with an attribute called 'data-values' the code would be:

  "link text",
  new { Model.Id },
  new { data_values = "some-value-for-the-data" })

Sourced from