HTML Helpers in 004.asp.net MVC

Source: Internet
Author: User
Tags html form actionlink

Original link: Http://www.codeproject.com/Articles/794579/ASP-NET-MVC-HTML-Helpers-A-MUST-KNOW

1. What is HTML Helpers

Imagine that the HTML helpers method returns a string, and yes, the string it returns is a string with an HTML tag. such as an IMG tag, a label, and other tags.

Developers with an ASP. NET development experience can use HTML helpers to write Web Form server controls because they have a common goal. But HTML Helpers is more lightweight because it has no view state and event model, using HTML Helpers alone, we can build a custom Helpers method.

Standard for 2.HTML Helpers

for ASP. NET MVC developers, it is very desirable to understand HTML helpers.

The standard HTML helpers can be categorized into the following two types:

A. URL Heplers = HTML links + Image Links

B. HTML Form Elements

3.MVC URL Helpers

Providing links in MVC is required, and there are two types available: HTML links and image links, which we discuss separately.

A. HTML Links

The Html.ActionLink () Help method is used to return an HTML link in a view that can be linked to a controller's method into the view.

1 @Html. ActionLink ("text Displayed", "ActionName")

The above method is an overload of the html.actionlink that can return a <a> tag, linked to the ActionName method of the Controller

1 <a href= "/controllername/actionname" > Text displayed </a>

B. Image Links

Url.action () Help method to return an image link

1 <a href= "@Url. Action (" Viewdetails ")" ></a>

Note: Although image link does the same thing, it links to a controller's Viewdetails method, but cannot include an IMG tag if the Html.ActionLink method is used

4.MVC HTML Form Element

To return the tags of various elements in HTML, MVC provides me with some of the following HTML helper methods

@Html. TextBox ("Strstudentname") renders:<inputID= "Strstudentname"name= "Strstudentname"type= "text"value="" />@Html. TextArea ("Stracademicbackground", "" "," Ten, "," null ") renders:<textareacols= " the"ID= "Stracademicbackground"name= "Stracademicbackground"rows= "Ten">@Html. Password ("Strpassword") renders:<inputID= "Strpassword"name= "Strpassword"type= "Password" />@Html. RadioButton ("Radgender", "Male", True) renders:<inputchecked= "Checked"ID= "Radgender"name= "Radgender"type= "Radio"value= "Male" />@Html. CheckBox ("Chkduespaid", True) renders:<inputchecked= "Checked"ID= "Chkduespaid"name= "Chkduespaid"type= "checkbox"value= "true" /><inputname= "Chkduespaid"type= "hidden"value= "false" />@Html. DropDownList ("Ddllevel", New SelectList (new [] {"1st Grade", "2nd Grade", "3rd Grade"}) renders:<SelectID= "Ddllevel"name= "Ddllevel">     <option>1st Grade</option>     <option>2nd Grade</option>     <option>3rd Grade</option></Select>

This article is only a few examples of usage, you should do forget to look at the small example of it.

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.