In HTML5, you can use the Data-property to represent user data, which can even be data in JSON format, which is very convenient for WEB front-end development.
In MVC's Razor, you can use anonymous objects to generate custom properties, but such properties cannot be checked by Razor syntax.
new{data-id= 1}
The compiler will report the error directly. The name ' data ' does not exist in the current context for the simple reason that the-number is used as an operator.
In fact, HtmlHelper provides a method anonymousobjecttohtmlattributes can already solve this problem, the description of the method is as follows.
Replaces underscore characters (_) with hyphens (-) in the specified HTML attributes.
We can use an underscore (_) directly in the anonymous object instead of the minus sign (-), and HtmlHelper will replace the underscore with a minus sign in the rendering.
For example, the following wording
@Html. TextBox ("username", "Noname", new{data_name= "Tom"})
The following HTML5 tokens are generated.
<input data-name= "Tom" id= "username" name= "username" type= "text" value= "Noname"/>
Razor adding HTML5 Properties