Detailed explanation of ASP. net mvc 2's moderate HTML helper Method

Source: Internet
Author: User

Existing HTML auxiliary methods

ASP. net mvc 1 released a set of HTML auxiliary methods to help generate HTML interfaces in view templates. For example, to output a text box, you can use the HTML. Textbox () method in your. aspx view template to write the followingCode:

The first parameter of the preceding helper method provides the name and ID of the text box, and the second parameter specifies its value. Then, the preceding helper method displays HTML like the following to the browser:

New Strong HTML helper Method

ASP. A common feature required by net MVC 2 is that we need to support a strong type of HTML auxiliary method. This auxiliary method uses lambda expressions to reference the model or view model passed to the view template. This can enable better visual inspection during compilation (defects can be found during compilation, rather than during runtime), and better support for code intelliisense in view templates.

The new strong-type HTML auxiliary methods are now built into ASP. net mvc 2. These methods use the "html. helpernamefor ()" naming convention. For example, HTML. textboxfor (), HTML. checkboxfor (), HTML. textareafor (), and so on. They support using lambda expressions to specify the name and ID of an element and the value to be displayed.

For example, in addition to the preceding html. Textbox () helper method, ASP. net mvc 2 is used, and now we can use the new HTML. textboxfor () helper method:

Note that we do not need to specify the "productname" string parameter above. Lambda expressions are quite flexible. In addition to values, we can also obtain the attribute/field name in our model object.

These HTML auxiliary methods are strongly typed. When compiling lambda expressions, we can also get full support for intelliisense in Visual Studio:

The output of the displayed HTML is the same as that of the HTML auxiliary method later bound to it:

List of strong HTML auxiliary methods built in ASP. net mvc 2

ASP. net mvc 2 provides built-in support for the following strong HTML auxiliary methods:

Auxiliary methods for HTML elements:

Html. textboxfor ()

Html. textareafor ()

Html. dropdownlistfor ()

Html. checkboxfor ()

Html. radiobuttonfor ()

Html. listboxfor ()

Html. passwordfor ()

Html. hiddenfor ()

Html. labelfor ()

Other auxiliary methods:

Html. editorfor ()

Html. displayfor ()

Html. displaytextfor ()

Html. validationmessagefor ()

I will discuss ASP later in this series. net MVC 2 improved the "auto-scaffold" function, for the new HTML. editorfor () and HTML. the displayfor () auxiliary method is further described. When we discuss the improved verification support in ASP. net mvc 2 in the next blog post of this series, we will also use the HTML. validationmessagefor () helper method.

Auxiliary method of strong HTML in scaffolding

When both vs 2008 and vs 2010 use the "add view" command "generate (scaffolding)" new strong-type view template, the new strong-type HTML auxiliary method is used by default.

For example, if we have a simple "productscontroller" class like the following, and there is an "edit" Action method, an editing form will be displayed for the "product" model class:

You can use Visual Studio to right-click the edit action method and select the "add view" context menu command to create a view template. We will choose to create an "edit" template, this template is generated using the product object (scaffolded ):

In ASP. net mvc 2, the default generated view template now uses a new strong HTML helper method to reference the product model object:

Conclusion

The strong-type HTML auxiliary methods included in ASP. Net MVC 2 provide a good way to get better type security in the view template. This facilitates a better compile-time check for your view (allowing you to detect errors during compilation rather than during runtime ), you can also edit a view template in Visual Studio to support more intelliisense features.

I hope this article will help you,

Related Article

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.