Razor adding HTML5 Properties

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.