Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand ASP.NET MVC Controller and View

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to understand ASP.NET MVC controller and view". In daily operation, I believe many people have doubts about how to understand ASP.NET MVC controller and view. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to understand ASP.NET MVC controller and view"! Next, please follow the editor to study!

First, controller related

A method with a method access level of public in the Controller class is called Action. If you do not want a method in the Controller class to be Action (which can be accessed in the address bar), there are two ways to do this: set the access level of the method to private and add property tags to the method [NonAction]

For functional modules such as new / modified modules, we often create two Action with the same name: one action is used to load the new / modified page, and the other action is used to deal with the form data submitted by the new / modified page. So how do you tell which action is called when?

We will load the action that is called when the page is modified, add the [HttpGet] attribute tag, and add the [HttpPost] attribute tag to the action that will be called when processing the form data submitted by the page.

The following sample code shows how to add students (action-related code).

How do I get the data submitted by the form in Action?

The data submitted in the form can be mapped by the parameters of Action.

Prerequisite for mapping: the value of the name attribute of the Html tag should be the same as the name of the parameter in action.

The following sample code demonstrates the mapping process (notice the part of the pink box mark):

How to map complex objects in Action?

Another equivalent way to write the mapping code above is to define the mapped attribute into an entity class so that we can add parameters of that entity type to action. The reference code is as follows:

How do I jump from one Action to another Action?

After the new or modified features are completed, you need to jump to the data list page, which can be achieved by using the following code:

Code = > return RedirectToAction ("ActionName", "ControllerName")

Second, view dependence

In the ASPX view engine, you can use the asp server control, but its function is limited to generating html code, and developers are not recommended to use asp control events. Therefore, it is recommended that developers write Html tags by hand.

But handwritten Html tags are time-consuming. Is there a better solution? The answer is to use the Html auxiliary method.

Html auxiliary method

The function of Html auxiliary method is to quickly generate corresponding html tags by calling C # method.

Html auxiliary methods can be divided into two categories: weak type html auxiliary methods and strong type html auxiliary methods.

Those ending with the For keyword are strongly typed helper methods. The following sample code demonstrates how both are called and the generated html source code.

Note: there are many overloaded forms of Html auxiliary methods, which can be chosen according to the actual needs in the development process. For example, to implement a single-line text box that contains class\ style\ onchange attributes and events, you can refer to the following code:

Emphasis: how to use the HTML auxiliary method to generate form tags?

The two Html helper methods for generating form tags are Html.BeginForm () and Html.EndForm (), but they are different from other Html helper methods when used, and the following sample code demonstrates their two uses:

How do I pass parameters in an URL address?

Scenario 1: map the student ID value to the parameter named id

Scenario 2: map the student ID value to the parameter named stuId

Scenario 3: stuId and name are included in the url address.

The usage code for these three scenarios is as follows:

Other in the actual development process, a small functional 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 Action in the controller. Summary of common Html auxiliary methods

Weak type

Strong type

Single-line text box

Html.TextBox ()

Html.TextBoxFor ()

Multiline text box

Html.TextArea ()

Html.TextAreaFor ()

Password frame

Html.Password ()

Html.PasswordFor ()

Check box

Html.Checkbox ()

Html.CheckBoxFor ()

Radio button

Html.RadioButton ()

Html.RadioButtonFor ()

Drop-down list box

Html.DropdownList ()

Html.DropdownListFor ()

Tiled list box

Html.ListBox ()

Html.ListBoxFor ()

Label

Html.Label ()

Html.LabelFor ()

Form form

Html.BeginForm () Html.EndForm ()

Hyperlink

Html.ActionLink ()

How to use drop-down lists in views

Step 1: construct the data needed in the drop-down list in action, with the data type IEnumerable.

Step 2: put the constructed data into the ViewData (ViewBag | TempData) attribute in Controller.

Step 3: use the Html.DropDownList helper method in the view view to generate the drop-down list, and the bound data can be obtained using the ViewData in the view View (in other words, the data placed in the ViewData in the Controller can be extracted from the ViewData attribute in the view View).

Use Html.DropDownList to bind Ienumerable in a view

At this point, the study on "how to understand ASP.NET MVC controllers and views" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report