Summary of ASP. net mvc htmlhelper class methods

Source: Internet
Author: User

Auxiliary and extension methods of the htmlhelper class:

(1) actionlink generates a specific controller behavior connection

<% = Html. actionlink ("edit", "edit", "book", new {id = model. ID}, new {@ class = "bookdetail"}) %>
Details:

Edit, Which is linktext, specifically the displayed string
Edit, corresponding to actionname;
Book, controller;
New {id = model. ID}, defined for the ID of the generated element;
New {@ class = "bookdetail"}, the tag element is added to the element

(2) antiforgerytoken generates a hidden form field, which can be used with the validateantiforgerytoken attribute to ensure that the request is not modified.

Details:

If you want to protect multiple forms of your application from mutual independence, antiforgerytoken will generate different anti-counterfeit signs

<% = Html. antiforgerytoken ("somearbitrarystring") %>

[Validateantiforgerytoken (salt = "somearbitrarystring")]
Public viewresult submitupdate ()
{
//... Etc
}

(3) attributeencode: Provides the htmlattributeencode function.

Details:

<% = Html. attributeencode (URL. Action ("login") %>

(4) dropdownlist is generated based on a pair of key-value pairs.

Details:

Html. dropdownlist (string name, ienumerable <selectlistitem> selectlist, string optionlabel, object htmlattributes)

Name: select the name of the element and bind it to the model attribute of the element view.

Selectlist: Option List,

Optionlabel: similar to "select ..." Optional default labels

Htmlattributes: attributes that can be added

Usage

 

Code

List <selectlistitem> items = new list <selectlistitem> ();
Items. Add (New selectlistitem {text = "Ming", value = "1 "});
Items. Add (New selectlistitem {text = "sorting ing", value = "2", selected = true });
Items. Add (New selectlistitem {text = "running", value = "3 "});
Viewdata ["listitems"] = items;

 

<% = Html. dropdownlist ("listitems") %>

(5) encode encoded strings to prevent cross-site scripting attacks

Usage
<% = Html. encode ("<SCRIPT src = \" J. js \ "> <Div background = 'javascript: Alert (''); '/> ") %>
Encoding result
& Lt; script src = & quot; J. JS & quot; & gt; & lt;/script & gt; & lt; Div background = 'javascript: Alert (''); '/& gt;

 

(6) Hidden generate hidden fields in the form

<% = Html. Hidden ("searchtype", 1) %>

Foreground source code:

<Input id = "searchtype" name = "searchtype" type = "hidden" value = "1"/>

(7) ListBox generates a drop-down Box Based on column key-value pairs

Usage:

Code

<% = Html. ListBox (
"Optionslist ",
New []
{
New selectlistitem
{
TEXT = "option 1 ",
Value = "opt1"
},
New selectlistitem
{
TEXT = "option 2 ",
Value = "opt2"
},
New selectlistitem
{
TEXT = "option 3 ",
Value = "opt3"
}
},
"Choose an option"
) %>

 

Foreground source code:

<Select length = "16" id = "optionslist" multiple = "multiple" name = "optionslist">

<Option value = "opt1"> Option 1 </option>
<Option value = "opt2"> Option 2 </option>
<Option value = "opt3"> Option 3 </option>
</SELECT>
(8) pasword form generation Password Field

Usage:

<% = Html. Password ("password", viewdata ["password"]) %>
(9) textbox generation text box

Usage:

<% = Html. Textbox ("name", model. Contact. Name) %>

Foreground source code:

<Input id = "name" name = "name" type = "text" value = "Alan"/>
(10) button to generate a button

The latest version 2.0.0.0mvc is no longer available.
(11) checkbox generation checkbox

Usage:

<% = Html. checkbox ("A") %>

Foreground source code:

<Input id = "A" name = "A" type = "checkbox"/>
(12) HTML form generated by beginform

Usage:

<% Using (html. beginform (model. postaction, "home", formmethod. Post ))
{%>

Foreground source code:

<Form action = "/home/Post" method = "Post">

(13) Image Generation of image tags

Reference an image

<% = Html. Image ("~ /Images/bigwave.jpg ") %> <br/>

Set Name and width
<% = Html. Image ("~ /Images/bigwave.jpg "," mywave ", new {width =" 30px "}) %> <br/>
(14) mailto generate an email connection

Simple Email:
<% = Html. mailto ("mailto: weflytotti@163.com % 22, % 22 email me! ") %>

With subject and body
<% = Html. mailto ("mailto: weflytotti@163.com % 22, % 22 email me! "," Sending you a note"
, "Hey-wanted to say hi! ") %>

(15) radiobutton (optional) buttons

<% = Html. radiobutton ("name", "value") %>
Renderaction presents a controller Behavior Method
Renderpartial is a partial view that is optional by another view engine.
Renderusercontrol
Routelink generates a specific route connection
Submitbutton presents a field similar to a button to submit a form
Submitimage presents a similar image submission form field
Textarea presents a text field
Validationmessage displays a verification message for a specific viewdata
Validationsummary presents a verification Summary of viewdata.

 

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.