The full name of MVC is the model View Controller, which is the abbreviation for models-Views (view)-Controller (Controler), a software design paradigm used to organize your code in a way that separates business logic from data presentation.
1. The top-most direct user-facing "view layer". It is the interface that is provided to the user.
2. The "Data Layer" (Model) at the bottom of the core is the data or information that the program needs to manipulate.
3. The middle layer is the "Control Layer" (Controller), which is responsible for selecting the data in the data layer according to the instructions entered by the user from the "View layer", and then doing the corresponding operation to produce the final result.
MVC is a bit like a canteen and can be divided into three parts. Part of the warehouse, is responsible for providing raw materials such as vegetables, this is the "Data Layer" (Model), and the other part is the rice window, which is its "view layer", which is responsible for the sale of meals, between the two through the chef this "control layer" (Controller) to remove raw materials from the "warehouse", After processing, put it on the "window" to sell.
The following is a small example of the respective roles and interactions of the three layers of MVC.
1. Create a new MVC project.
2. Create a new user class under the Models folder to simulate the data.
public class user {public string UserName {get;set;} public string Password{get;set;} Public override String ToString () { return "username=" +this.username+ ", password=" +this.password; } }
3. Create a new controller under the Controllers folder to retrieve the data from the User.cs and pass the processed data to the index.cshtml view.
Namespace mymvctest.controllers{public class Myusercontroller:controller {////GET:/myuser/ Public list<models.user> InitData () {list<models.user> List = new List<models.user> ;() {New Models.user () {username= "1", password= "1"}, new Models.user () {username= "2 ", password=" 2 "}, new Models.user () {username=" 3 ", password=" 3 "}}; return list; Public ActionResult Index () {//Create a StringBuilder System.Text.StringBuilder sbhtml = New System.Text.StringBuilder (4000); Initialize data list<models.user> List = InitData (); Gets the data from the list and deposits it into the sbhtml list. ForEach (D + = {sbhtml.appendline ("<div>" +d.tostring () + "</div>"); }); Use VIEWBAG to transmit data to the index.cshtml view with the same name viewbag.htmlstr = Sbhtml.tostring (); ViewBag is a DThe Ynamic type collection, which can dynamically add any type of property and value of any name to return View (); } }}
4. Add a view to display the data in the index.cshtml view.
@{ Layout = null;} <! DOCTYPE html>
5. Configure the route, set the newly added view as the default startup view, and locate the RouteConfig.cs file under the App_start file for modification.
Summary: view is provided to the operator interface, the data or information that the model provider needs to manipulate, The controller is responsible for processing the data and passing it into the view.
SOURCE download
mvc--Getting Started + The simplest small instance