. Net MVC Learning (1)

Source: Internet
Author: User

. Net MVC Learning (1)

It is also interesting to develop an interplanting model. At the same time, DRP has learned something that has never been learned before but has been heard many times-the MVC Architecture Pattern in Asp. Net.

1. What is it?

  

MVC(Model-View-Controller, Model-View-Controller mode), similar to Layer 3, used to represent a software architecture mode. In this mode, the system implementation is divided into Model, View, and controller Controlller. Model: database operations and general business logic. View: displays interaction with users. Controller: Processes requests and Response requests. It is the intermediary for the View layer to access the Model layer.

II. Specific implementation.

 

A specific example shows the specific implementation of MVC.

First, create an MVC 3 web application.



PS: MVC3 and MVC4. I checked the relevant materials and understood that the overall architecture and implementation did not change much, only a specific class or interface is changed.

Because the general process is to Request -- → Controller -- → Model and then return it to the View for display, it is implemented from the bottom layer.


1) Model

Create a Login_BS class in the Model folder. Because it is a simple implementation, database operations are not performed, but the MVC implementation process and principles are understood.


 public class Login_BS    {        public bool Login(string username, string password)        {            if (username == "1" && password == "1")            {                return true;            }            else            {                return false;            }        }    }


2) Controllers

Right-click the Controllers folder and add the controller.



Note: The first part of the Controller is the name called by the Controller, that is, the place where the form is submitted is Login.

The Code is as follows:


Public class LoginController: Controller {// GET:/Login/public ActionResult Index () {return View ();} [AcceptVerbs (HttpVerbs. post)] // form submission method public void index (string username, string password) {Models. login_BS loginbs = new Models. login_BS (); // username = Request. queryString ["username"]; this is the get method to submit and obtain data // password = Request. queryString ["password"]; if (loginbs. login (username, password) {Response. write ("Login successful" + username);} else {Response. write ("Logon Failed" + username );}}}


Right-click the Index () method to add a view.



Then, a Login folder will be automatically added under the View folder. Under the Login file, there will be a file named Index (obtained by yourself when adding the View) with the suffix cshtml, this is the View corresponding to the controller we added. Add specific forms and controls:

The Code is as follows:

3) View


View code: @ {Layout = null ;}    Index                

4. self-understanding

Run the program directly and we will find the 404 page. Why is there a View not displayed? In fact, this involves the specific division of labor in the MVC model. As discussed in the Model2 mode in DRP, does the page directly drawn by JSP in strictly model2 mode (MVC) mode not exist? Because each page to be displayed must be rendered by a servlet before being drawn by the jsp, that is, the servlet is not accessed, there will be no Html pages drawn by the corresponding jsp (it is purely your own understanding. If you have different opinions, you can exchange them ). Return to Asp.net. In MVC, the address accessed by the user is not mapped to the corresponding file on the server, but to the corresponding ActionMethod in the corresponding Control. The ActionMethod determines what information the user will return. However, the routing system completes the work of ing the user access address to the corresponding Action (of course, the corresponding file), many of which are complicated. net automatically, and developers need to tell.. net user access address and corresponding Action ing relationship. We directly run the program and did not map it to the corresponding Action method. Therefore, it is normal to display the 404 page.
If you want to display a specific page. You can add the corresponding Action Method to the address bar, that is, the address/Login result submitted by the form.





<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Keys + keys/keys + keys/Uyr7E2sjdtcTSu7j21NjM5aOsy/keys + yrldtm1_n2jrmirsr/Tyb/keys + oaghoaghpgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20140821/20140821104809184.png" alt = "\">



While MVC is:





Keep one question: does the View interact with the Model through the "consent" of the Controller? Or does the View interact with the Controller, while the Controller interacts with the Model?


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.