The full name of MVC is the model View Controller, which is the abbreviation for the models-view-controller, a software design paradigm that organizes the code with a method of business logic, data, and interface display separation. Aggregating business logic into a single component does not require rewriting business logic while improving and personalizing the interface and user interaction. MVC is uniquely developed to map the traditional input, processing, and output functions in a logical graphical user interface structure.
Get started quickly . Today we are going to create an MVC project from Scratch and introduce the relevant knowledge points. Let's get straight to the point. I do a simple demo is the most basic, specific projects will also be based on the specific business to do different options. But it does not affect our understanding of the principles and processes.
The demo implements the interface display text through MVC's small project.
One add controller
Open vs New Project. I chose the mvc4.
This is how it looks when it's new. 1 we see it automatically has generated the models views controllers these three folders. That's what we call MVC.
Next we set up the controller. Right-click Controllers, add-controller.
Note that the suffix controller cannot be changed, you can change the selected blue part according to your needs.
Knowledge Point: At this point we see that the new built-in controller class is not the same as the normal class it is inherited controller. When we looked at the controller, we found its physical path in the folder of our new project, which means it was automatically added, and it was automatically referenced.
2 There is also an action method that is automatically generated at the same time. It is used to process the business and operational databases.
Two new class codes in model are as follows.
Three add view
Right-click your own new Defaultcontroller class in the code. Add a view. The default view name and the name of your own controller class are the same without modification.
Knowledge Point: Once you've set up, you'll find that the view is automatically added below the Views folder. An action in a controller can correspond to a view.
Four Code Parts
With the Controller model views after the three new classes, add the corresponding code in the controller as follows.
The point is I'm going to upload the value of the controller to the view.
① uses ViewBag to get the content to be passed in the controller.
Using mvcdemo.models;using system;using system.collections.generic;using system.linq;using System.Web;using System.web.mvc;namespace mvcdemo.controllers{public class Testcontroller:controller {//Create a data set (false data) GET:/test///<summary>///initialization data set///</summary> public list<models .apple> InitData () {list<models.apple> List = new list<models.apple> () {n EW Apple () {id=1,name= "Red apple"}, new Apple () {id=2,name= "Green Apple"}}; return list; }///<summary>//action method///</summary>//<returns></returns> Public ActionResult Index () {System.Text.StringBuilder sbhtml = new System.Text.StringBuilder (40 00); Handle current business such as reading database judgment, etc.//create a data set (pseudo data) list<models.apple> List = InitData (); Iterate through the collection to generate HTML code to deposit sbhtml List. ForEach (D + = {sbhtml.appendline ("<div>" +d.tostring () + "</div>"); }); Use ViewBag to transfer an index cshtml view with the same name//viewbag is a collection of dynamic types that can dynamically add properties and values of any name of any type VIEWBAG.HTMLSTR = SbH Tml. ToString (); return View (); } }}
② Add a statement to the view directly as follows. Can.
@{ Layout = null;} <! DOCTYPE html>
At this point we can right-click the index right-click to see the effect in Page Inspector.
Summary: Want to knock the code over and think. Knocking without thinking is reckless without knocking.
Quick Start example of MVC