asp.net MVC Controller and view _ Practical tips

Source: Internet
Author: User
Tags html tags

First, the controller related

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

                  

function modules such as new, modify, and so on, we tend to create 2 actions with the same name: one action to load the new \ Modify page, and the other action to process the form data that the new \ Modified page submits. So how do you tell which action to call?

We will load the Add | Action that was invoked when modifying the page, add the [HttpGet] attribute tag, and add the [HttpPost] attribute tag to the action that will be invoked when the form data submitted by the page is processed.

The following example code demonstrates 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 parameters of the action.

Prerequisite for mapping: The Name property value of the HTML tag and the names of the parameters in the action are consistent.

The following example code demonstrates the mapping process (note the portion of the Pink box mark):

                  

How do I map complex objects in action?

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

                  

How do I jump from one action to another?

After the new or modified feature is completed, you need to jump to the data listing page, which we can implement by:

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

Second, view-related

In the ASPX view engine, you can use an ASP server control, but its role is only to limit and generate HTML code, not to recommend events for developers to use ASP controls. It is therefore recommended that developers hand-write HTML markup.

But are handwritten HTML tags time-consuming and have a better solution? The answer is to use HTML helper methods.

HTML helper Methods

The role of HTML helper methods is to quickly generate the corresponding HTML markup by invoking the C # method.

HTML helper methods are grouped into 2 broad categories: weakly-typed HTML helper methods and strongly-typed HTML-assisted methods.

These are strongly typed helper methods that end with the FOR keyword. The following example code shows how the two are invoked, and what HTML source code is generated.

                  

Note: HTML helper methods have many forms of overloading that can be selected for actual needs during development. For example, to implement a Single-line text box that contains Class\style\onchange properties and events, you can refer to the following code:

                  

Emphasis: How do I generate a form tag using HTML helper methods?

The two HTML helper methods that generate form tags are: html.beginform () and Html.endform (), but they are used differently from other HTML helper methods, and the following sample code demonstrates two uses:

                  

How can I pass parameters in a URL address?

Scene One: Map the student ID value to the name of the ID parameter

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

Scene Three: The URL address includes stuid and name two parameters

The code for using 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, add students \ Modify students \ Query Student list \ Delete students can correspond to the Controller action. Common HTML helper Method rollup

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 ()

Hyperlinks

Html.ActionLink ()

How to use a drop-down list in a view

Step one: In the action, construct the data you want in the Drop-down list, and the data type is ienumerable<selectlistitem>.

Step Two: Put the constructed data into the ViewData of the controller (viewbag| TempData) attribute.

Step three: Use the Html.dropdownlist helper method in view views to generate a Drop-down list, and the bound data can be obtained by using the ViewData in view views (in other words, the data that is placed in the controller into the ViewData, which can be observed in view Data property).

                  

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

                  

Related Article

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.