Study MVC Step by Step (a) what is MVC?

Source: Internet
Author: User

When we began to gradually put the web development above the net platform from WebForm to MVC development. We're always trying to figure out what MVC is. In fact, MVC is not a net-specific development technology. But a model of software development. Back in the last world of the 80 's. Xerox Parc is a software design pattern invented by the programming language Smalltalk-80, which has been widely used. So what is MVC?

MVC is a pattern of creating WEB applications using MVC (Model View Controller-View-controller) Design:
    • Model (models) represent the core of the application (such as a list of database records).
    • View displays data (database records).
    • The controller (Director) processes the input (writes to the database record).

The MVC pattern also provides complete control over HTML, CSS, and JavaScript. model (models)Is the part of the application that handles application data logic. Typically, model objects are responsible for accessing data in the database. view (views)Is the part of the application that handles the display of data. Typically views are created from model data. controller (Controllers)Is the part of the application that handles user interaction.    Usually the controller is responsible for reading data from the view, controlling user input, and sending data to the model. Looking at the above description, it seems indefinitely.     Well, let's use our customary webform to demonstrate what is called MVC.   First we create an empty Web site with three files shown in 1.1 for a model class (at the site new Xxx.cs is usually placed under the App_Code folder that does not allow external access). A general handler for a mvccontroller. (. ashx) a normal HTML page called view. (. html)

I write the code one at a time in the above three files:

Model.cs

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;/// <summary>///Summary description of Model/// </summary> Public classmodel{ PublicModel () {//        //TODO: Add constructor logic here//    }     Public intId {Set;Get; }//ID Number     Public stringName {Set;Get; }//name     Public intAge {Set;Get; }//Age     Public BOOLGender {Set;Get; }//Sex     Public stringAddress {Set;Get; }//Address     Public stringPhone {Set;Get; }//Phone number     Public stringEmail {Set;Get; }//e-Mail     Public stringCompany {Set;Get; }//company}
View Code


View.html

<!DOCTYPE HTML><HTMLxmlns= "http://www.w3.org/1999/xhtml"><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"/>    <title></title>    <style>form{Margin-left:Auto;Margin-right:Auto;width:500px;        }    </style></Head><Body>     <form>      <Table>        <TR>            <TD>ID Number:</TD>            <TD>@id</TD>        </TR>          <TR>              <TD>Name:</TD>              <TD>@name</TD>          </TR>          <TR><TD>Age:</TD>              <TD>@age</TD>          </TR>                  <TR>              <TD>Phone number:</TD>              <TD>@phone</TD>          </TR>          <TR>              <TD>E-Mail:</TD>              <TD>@email</TD>          </TR>          <TR>              <TD>Home Address:</TD>              <TD>@address</TD>          </TR>          <TR>              <TD>Company Name:</TD>              <TD>@company</TD>          </TR>      </Table>  </form></Body></HTML>

Mvccontroller.ashx

<%@ WebHandler language="C #"class="Mvccontroller"%>usingSystem;usingsystem.web; Public classMvccontroller:ihttphandler { Public voidProcessRequest (HttpContext context) {context. Response.ContentType="text/html";//returns the HTTP response headerModel M=NewModel () {id=410102, name="Rocky Ren", age= -, gender=false, phone="18301412747", email="[email protected]", company="Henan Cloud and data", address="Zhengzhou, Henan province", };//model entity file. Get the model and initialize the various properties inside.         stringViewPath = context. Server.MapPath ("view.html");//get the static page path of the view        stringviewhtml = System.IO.File.ReadAllText (ViewPath);//get the static page HTML string for the viewviewhtml= Viewhtml.replace ("@id", M.id.tostring ()). Replace ("@name", M.name). Replace ("@age", M.age.tostring ()). Replace ("@phone", M.phone). Replace ("@company", M.company). Replace ("@email", M.email). Replace ("@address", m.address);//Replace all @xx values in view.html with the model entity object. context. Response.Write (viewhtml);//output to the foreground page                                      }      Public BOOLisreusable {Get {            return false; }    }}
View Code

First we look at the page effect. Request Mvccontroller


When our browser requests mvccontroller. An HTTP request passes through our net handler to reach the class that implements the IHttpHandler interface is our generic handler. This interface has a method public void ProcessRequest (HttpContext context) in our net all processing requests are handled by this method, Where the parameter HttpContext contains the processing of our request response.

Through, you see our Mvccontroller this class inside the method ProcessRequest inside that is processing the return page needs data. It also returns the dynamic HTML content by locating the HTML page and loading the data.

M:model.cs the container that contains the data. Provides data items for the application.

v:view.html Front desk Display page. The content returned to the browser.

C: Handles page requests and returns execution of HTTP requests.

The essence of MVC is to isolate processing and display. Divide the project into model View and controller, this separation of tasks (also known as separation of concerns)

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.