MVC5 forms and HTML helper methods

Source: Internet
Author: User

Tag: Represents the box system HTML selected Web verbose access type

① use of the form

In Webfrom you create a page, the page will appear <from></from>.

1,action and Method Properties

The action represents the purpose, method represents the table only son value way Get/post, the action defaults to the current URL, method default is: Get

2,enctype generally need to set this property to allow forms to submit multimedia types

②html helper method if BeginForm () represents a post submission to the current URL.

1, automatic encoding

How the 2,html helper method works

The type of HTML attribute is system.wbb.mvc.htmlhelp<dynamic>, in addition to the system's own auxiliary methods, we can also customize the helper method, how to customize, we say in detail later.

3, adding input elements

1.html.textbox and Html.textarea

2.html.label

3.html.dropdownlist and Html.listbox

Both are returned <select/> elements, DropDownList allow single selection, and the ListBox is a multiple selection.

Typically, the SELECT element has two functions

1, Show selectable list

2, show the current value of the field

The drop-down list requires a collection of SelectListItem objects with all the options available, each of which contains the text, value, and selected three properties in each of the SelectListItem objects.

4.html.validationmessage

When an error occurs in a particular field in the Modelstate dictionary, you can use the Validationmessage helper method to display the appropriate error message.

5. Auxiliary methods, models, and view data

Match data

Use the values in ViewBag to name the TextBox helper method

Object Properties

Strong type:

If we want to avoid the above auto-lookup data, we can provide a display value.

6. Strongly-typed helper methods

Strongly typed we can pass a lambda expression to show that our helper methods are multiple "for"

7. Auxiliary methods and model metadata data annotations to change the display

8. Template-Assisted methods

Generally use editor instead of TextBox

9. Auxiliary methods and Modelstate

Modelstate All validation errors detected during the storage model binding, and the original values that the user submits to follow the new model.

The helper method uses the name expression as the key to find in the Modelstate dictionary. If the lookup value is already in modelstate, the secondary method replaces the current value in the view data with the value in Modelstate. , the model binding fails and an error is displayed.

10. Other input helper methods

11. Rendering Helper Methods

Html.ActionLink () and Html.routelink ()

12.URL helper Methods

①action②content③routeurl

13. Partial View helper methods

Using partial views has the following advantages: 1. You can abbreviate the code. 2. The page code is clearer and better maintained.

There are several ways to load a partial view in a view, including: Partial (), renderpartial (), Action (),renderaction(), Renderpage () methods

Partial and RenderPartial methods

Razor syntax: @Html. Partial () and @{html.renderpartial ();}

Difference: Partial can directly output the content, it is inside the HTML content is converted to string character (mvchtmlstring), and then cached, and finally output to the page at once.    Obviously, this conversion process will reduce the efficiency, so usually use renderpartial instead. Indicates crawl page class capacity, cannot catch method

Some data is passed in the action of partial and called in partial views of partial.

Loads the partial view in the index view. The following two methods can be loaded successfully, but the second is more efficient, so we use html.renderpartial to load the view

After running the page content is loaded, but not loaded into the ViewBag variable inside the action.

Renderaction and Action

Razor Syntax:

@{html.renderaction ();} With @html.action ();

Difference: Action is also a direct output, and as with Partial, there is a process of conversion. Renderaction output directly to the current HttpContext efficiency.

In addition to this, action accesses the action in the controller and executes the business within the action, compared to partial.

After running, the values in the ViewBag object are also captured in the

Renderpage and RenderPartial

Razor Syntax:

@{html.renderpartial ();} With @RenderPage ()

Difference: You can also use Renderpage to render parts, but it cannot use the Model and ViewData of the original view, only passed by parameters. RenderPartial can use the Model and ViewData of the original view.

Add the code and run it, @RenderPage does not execute the action.

14.Encode and Raw

Html.encode is used to encode special characters on a page. (The dialog box is not played, coded)

Standard notation: <%= html.encode (viewdata["script"])%>

Simple wording: <%: viewdata["script"]%> (Master)

Html.raw the string as it is on the page. (The dialog box will be played on the page)

Standard notation: <%= html.raw (viewdata["script"])%>

Simple wording: <%= viewdata["Script"]%> (Master)

<%:%> will encode the string

<%=%> The full output of the string on the page, not encoded

In addition to the self-bringing of these assignment methods, we can also customize, this is described later.

MVC5 forms and HTML helper methods

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.