The ASP. NET MVC 3.0 Knowledge Point collation-----(4). HtmlHelper (Html helper method) Introduction

Source: Internet
Author: User
Tags actionlink

in the View view, the type of HTML is System.web.mvc.htmlhelper<t>, and all the helper methods are required and modelstate interaction. So, what is Modelstate? It is an accessory to model binding and contains all the validation errors detected during model binding. And the original value of the user Submission update model with the arrival.
In this blog post, we mainly introduce the main functions and use methods of some commonly used HTML helper methods.

1. Html.BeginForm () and Ajax.beginform ().

html.beginform ():
The same as the traditional form submission, mainly to generate form values, if the form is strongly typed, when the form is submitted, the table cell name name is automatically populated with the property values of the type entity in the strongly-typed view, and in the form, if we are strongly-typed views, You can use @model.username to send values directly to the specified location.

typically used with the using {}, otherwise add html.endform ()to the end of the form. method is divided into: post and get: after committing, the variable can be obtained in the URL address bar. When a get is committed, it does not change the state of the server, and the client repeatedly sends a GET request to the server without adversely affecting the server. Post: all elements in the form are submitted, and the POST request alters the state of the server, so the client repeatedly sends a POST request to the server that has an impact on the server. How to use: 
" Method Name " " Controller name " New {target="_blank"" Form class name, easy to define style ", @id ="  ID Name of the form, easy to get TABLE element "})") {  }
Equivalent to:
<action= "Controller name/Method name"= "Method submitted"= "Class name for the form "  ID=" id name of the form "  target=" _blank "></form >
  Ajax.beginform ():support Asynchronous form submission, you can easily complete an asynchronous form submission action by using MVC's own Ajax.beginform  .
@using (Ajax.beginform (new="userlogoncontainer"= " Post " , onsuccess=""      // submit to current page, submit by post, Asynchronously updates a block of content with module ID userlogoncontainer

can also be submitted to the action of the specified controller.

@using (Ajax.beginform ("Action","Controller",NULLNewajaxoptions{Updatetartetid="Userlogoncontainer", HttpMethod="Post", onsuccess=""      })) //commits to the currently specified controller's action, commits the post, asynchronously updates the block of content with module ID userlogoncontainer

2. Html.validationsummary ()

mainly used to(1). Displays the prompt error message after the background modelstate.isvalid validation failed. (2). Or background validation passed, but some logical validations did not pass. For example: When the user logs in, the user name or password is wrong. You can add the error message manuallyView page:
@Html. ValidationSummary (True, "Login was unsuccessful. Please correct the errors and try again. ")
Controller can write:
Modelstate.addmodelerror ("" "Theuser name or password provided is incorrect. ");

3.html.validationmessage ()

functions are similar to Html.validationsummary (), which is used to display error messages when authentication fails in the Modelstate dictionary. How to use: The controller writes:
Modelstate.addmodelerror ("Title","whata terrible name! ");
use in view
@Html. Validationmessage ("Title")

4.html.textbox (), Html.textarea ()

It is mainly used to render HTML textbox and textarea. How to use:

< BR /> infomation ")
Equivalent to:
<inputtype= "text"ID= "title"name= "title">textbox Information</input><textareaID= "Textareatitle"name= "Textareatitle">textarea&lt;BR/&gt;Information</textarea>
The content of the output is HTML-encoded.
5.html.label ()the label helper method returns a <label/> elementuse: Controller:
[DisplayName ("Genre")]    // What's displayed  Public int genreid{get;  Set;}

View:

@Html. Label ("Genreid")
Equivalent to:
<for= "Genreid">Genre</label>

6.html.dropdownlist (), Html.listbox ()

Two helper methods return a SELECT element, DropDownList only allows multiple selections, while a ListBox allows multiple selections
( The value of the multiple attribute in the rendered markup is set to multiple), and the background-bound data source generally uses the SelectListItem type.

New selectlistitem{    = "Display content",    "corresponding value ",    = bool Value (checked)}

7.html.editor ()

Usage: Custom Editor editor.
For specific use, refer to this article: http://hi.baidu.com/scorpio_jone/item/3080aefc8133c713a7298843

8.html.hidden ()

Used to render hidden input elements, which are typically used to display hidden fields on a page.
Usage:

@Html. Hidden ("wizardstep""1")

=

@Html. hiddenfor (w = w.wizardstep)
(Ps:hidden and hiddenfor specific what difference, we will mention later.) )corresponding to the HTML:
<id= "WizardStep"  name= "WizardStep"  type= " Hidden "  value=" 1 "/>

9.html.password ()

Used to render password fields. He does not retain the value of the commit and the use of a password mask other than the basic and TextBox auxiliary methods. Usage:
@Html. Password ("UserPassword")

=

@Html. passwordfor (w = w.userpassword)
corresponding to the HTML:
<id= "UserPassword"  name= "UserPassword"  type= " Password "  value=" "/>

10.html.radiobutton ()

Used for rendering radio buttons, which are generally combined Use。 As follows: Select Gender (male/female)
Usage:
@Html. RadioButton ("Gender", "Male") @Html. RadioButton ("Gender", "Female", True)
=
@Html. radiobuttonfor (M = M.gender, "Male") @Html. Radiobuttonfor (M = M.gender, "Female")

11.html.checkbox ()

Use to render check box buttons (checked, unchecked). It is The only rendering of two input elementsThe auxiliary method. Usage:
@Html. CheckBox ("isChecked")
Corresponds to:
<name= "isChecked"  ID= "isChecked"  type= " CheckBox "  value=" true "/><name=" isChecked "  type=" hidden "  value=" false "/>
(The reason for this is that when unchecked, make sure you have a value to commit.) ) 12.html.actionlink (), Html.routelink ()Html.ActionLink ():(1). Used to render a hyperlink that specifies another controller action, as in the previous BeginForm helper method. Usage:
@Html. ActionLink ("link Text displayed by name", "Anotheraction Controller method name to be submitted")
Corresponds to:
<href= "/home/anotheraction">Link Text</a >
(2). A different controller is used:
@Html. ActionLink ("Link Text", "Anotheraction", "Anothercontroller")
Corresponds to:
<href= "/anothercontroller/anotheraction">Link Text</ a >
(3). If you want to pass a parameter to the action, you can use:
@Html. ActionLink ("Link Text", "Anotheraction", "Anothercontroller", new{ID = 123})

Corresponds to:

<href= "/anothercontroller/anotheraction?id=123">Link Text </ a >
Html.routelink ():and Html.ActionLink () follow the same pattern. However, Html.routelink () can receive the route name instead of receiving the controller name or the method name. such as:
@Html. RouteLink ("Link Text", new {action = "anotheraction"})

13.html.partial (), html.renderpartial ()

(1). Html.partial is used to render a division into a string.(2). Html.renderpartial is to embed the user control directly on the interface. such as:
@{html.renderpartial ("logonusercontrol"

Or

@{html.renderpartial ("~/areas/comm/views/shared/logonusercontrol.ascx");}
The two are similar:@Html. Partial ("Albumdisplay") (more convenient) = @{ html.renderpartial ("Albumdisplay"); (good performance) 14.html.action (), html.renderaction ()Html.renderaction: Invoking a user control through an action in the controllerController:
 Public ActionResult UserControl () {    return  Partialview ();}
View:
@Html. Renderaction ("UserControl","Controller")

15.url.action ()

The URL helper method is similar to HTML actionlink and RouteLink. Instead of returning the constructed URL in the form of an HTML tag, he returns a URL in the form of a string.
< span >     @Url. Action ("Browse", "Store", new{genre = "Jazz"}, NULL)</span>
corresponding to the HTML
< span > </span>      

16.url.content ()

<src= "@Url. Content ("~/scripts/jquery-1.10.1.min.js ")" type= " Text/javescript "></script>

In addition to the HTML helper methods described above, there are some of the HTML for methods mentioned above. So what are the similarities and differences between the For method and others?
The For method can be rendered by means of a lambda expression, in combination with the model entity. notation: @Html. labelfor (M = M.genreid) and so on.

Reference: Http://www.tuicool.com/articles/MRJNre

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.