ASP. NET MVC Advanced One

Source: Internet
Author: User

First, controller-related
    • In the Controller class, the method accesses a public-level method, which is the behavior (action).
    • If you do not want a method in the Controller class to be an action (which can be accessed in the Address bar), there are two ways to implement it:
    1. To set the access level of a method to private
    2. Adding an attribute tag to a method [Nonaction]

    • such as new \ Modify function modules, we tend to create 2 names of the same action: an action to load the new \ Modify page, and another action to process the new \ Modify the page submission of the form data. So how do you tell which action to invoke?

We will load the action that was called when the page was modified, add the [HttpGet] attribute tag, and the action that is invoked when the form data submitted by the page is processed, and the [HttpPost] attribute tag is added.

The following sample code shows how to implement a student's new (action-related code).

                  

    • How do I get the data submitted by my form in action?

The data that is submitted in the form can be mapped by the action's parameters.

Prerequisites for mapping : The Name property value of the HTML tag is consistent with the names of the parameters in the action.

The following sample code demonstrates the process of mapping (note the part of the Pink box tag):

    • How do I map complex objects in action?

For the above mapping code, we can also have another equivalent notation, is to define the mapped attribute into an entity class, so that we can add the parameter of the entity type in the action, the reference code is as follows:

                  

    • How can I jump from one action to another?

When the new or modified feature is complete, you need to jump to the data list page, which we can do with the following code:

Code = return redirecttoaction("ActionName", "Controllername").

Second, view-related
    • In the ASPX view engine, you can use ASP server controls, but it is only limited to generating HTML code, and it is not recommended for developers to use the ASP control's events. It is recommended that developers manually write HTML tags.

But handwritten HTML markup is time-consuming, and is there a better solution? The answer is to use the HTML helper method.

    • HTML helper Methods

The purpose of the HTML helper method is to quickly generate the appropriate HTML markup by invoking the C # method.

The HTML helper methods are divided into 2 categories: The weakly-typed HTML helper method and the strongly-typed HTML helper method.

Where the FOR keyword ends with a strongly typed helper method. The following sample code shows how both are called and the generated HTML source.

Note: HTML helper methods have many kinds of overloaded forms, which can be selected according to actual needs during the development process. For example, to implement a single-line text box containing Class\style\onchange properties and events, refer to the following code:

emphasis: How to use HTML helper Method Generation form Mark ?

The two HTML helper methods for generating a form tag are: Html.BeginForm () and Html.endform (), but they differ from other HTML-assisted methods when they are used, and the following sample code demonstrates two of their uses:

    • How do I pass parameters in the URL address?

Scenario One: Map the student ID value to the name ID parameter

Scenario Two: Mapping the student ID value to the name Stuid parameter

Scenario Three: URL address includes stuid and name two parameters

The usage code for these three scenarios is as follows:

                  

Other
    • In the actual development process, a small function module corresponds to a controller. For example, student information management is a controller, new students \ Modify students \ Query Student list \ Delete students can correspond to the action in the controller.
    • Summary of common HTML helper methods

Weak type

Strongly typed

Single-line text box

Html.textbox ()

Html.textboxfor ()

Multi-line text box

Html.textarea ()

Html.textareafor ()

Password box

Html.password ()

Html.passwordfor ()

check box

Html.checkbox ()

Html.checkboxfor ()

radio button

Html.radiobutton ()

Html.radiobuttonfor ()

Drop-down list box

Html.dropdownlist ()

Html.dropdownlistfor ()

Tile list Box

Html.listbox ()

Html.listboxfor ()

Label

Html.label ()

Html.labelfor ()

Form form

Html.BeginForm () Html.endform ()

Hypertext links

Html.ActionLink ()

    • How to use a drop-down list in a view

The first step: construct the data you want in the drop-down list in action, with the type ienumerable<selectlistitem>.

Step Two: Put the constructed data into the controller's ViewData (viewbag| TempData) property.

The third step is to use the Html.dropdownlist helper method to generate the drop-down list in view views, and the bound data can be obtained using the ViewData in the Views view (in other words, the data that is placed in the controller into ViewData can be in the view Data attribute).

                  

Use html.dropdownlist bindings in the View ienumerable<selectlistitem>

                  

ASP. NET MVC Advanced One

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.