The MVC framework for Web development

Source: Internet
Author: User
Tags zend framework

what is MVC framework?


Here I briefly explain, want to know in detail MVC the small partners can on the Baidu Encyclopedia, or find the corresponding books, carefully study and understand, but to illustrate the point is that fully understand MVC It's not an easy thing to do.


MVC are all called Model View Controller , is the model - View - the abbreviation of the controller, a software framework mode, the popular point is that the application input, processing and output decoupling, so that M and the V Separation of the implementation code so that the same program can use a different representation.


In a nutshell, MVC The contents of the three core components of the framework, i.e. Model , View and the Controller . They each handle their own tasks, so that different developers can develop views, controller logic, and business logic at the same time.

Model : refers to the business code and data manipulation code to be processed

View : A view is primarily the ability to interact with users and to display data

Controller seen as Model and the View Bridge, accepting user input and invoking models and views to complete the user's needs.

How to use MVC programming?


since MVC is a universal framework, then we can VS to create a use MVC framework of the solution, and then write your own code to implement the required functionality.


First, open VS , the new project looks like this:


Click OK and you'll see that the right side of the Solution Explorer has been created for us automatically MVC the framework, as shown in:


Then we can create models, controllers, and Views under the corresponding files.

to see a small example, we're in the folder Controllers Right-click to add a controller as shown in:



and then in Models add an entity class to the file with the following code:

<span style= "FONT-SIZE:18PX;" >namespace mvcblog.models{public    class Dog    {public        int ID {get; set;}        public string Name {get; set;}        public override string ToString ()        {            return ' id= ' + this.id + ", name=" + this. Name;}}    } </span>

So that we can write the code in the controller, the code is as follows:

<span style= "FONT-SIZE:18PX;"        >namespace mvcblog.controllers{public class Homecontroller:controller {////GET:/home/            Public list<models.dog> InitData () {list<models.dog> List = null; List = new List<models.dog> () {new Dog () {id=1,name= ' Tibetan Mastiff}, New Dog () {id=2            , name= "Shepherd Dog"}, new Dog () {id=3,name= "Chihuahua"}, New Dog () {id=4,name= "Husky"}};        return list; } public ActionResult Index2 () {System.Text.StringBuilder sbhtml = new System.Text.StringBuilder            (4000);            list<models.dog> list = InitData (); List.            ForEach (D + = {sbhtml.appendline ("<div>" + d.tostring () + "<div>");            });            Viewbag.htmlstr = Sbhtml.tostring ();        return View (); }}}</span>

Finally, let's create the View by Index2 method, right-click, select Add View, as shown in:


we will see the views folder under the Home The folder has a name of index2.cshtml file, this is the view file, we can write a line of code in the view to call the controller method, the code is as follows:

<span style= "FONT-SIZE:18PX;" >@{    Layout = null;} <! DOCTYPE html>

see below How to run the program, run the traditional Web The application we entered in the address bar of the browser is http://localhost:60063/ views/ Home/index 2.aspx , while we use MVC The application is not written like this, we only need to invoke the corresponding view, in the browser is entered Http://localhost:60063/Home/Index 2 , the result after carriage return is as follows:


Summary: For MVC Framework, many people will think of the three-layer framework, they do have a similar place, are layered decoupling of the application, but in contrast, MVC The three parts of the frame are decoupled completely, they are independent of each other, changing one does not affect the other two, so that the coupling is low, so the greatest benefit is the high maintainability. MVC has other advantages, of course, there are many shortcomings, interested in the information can be consulted.


It is also important to note that many people are not very clear about the concept of frame patterns and design patterns, and are particularly prone to confusing both. So many people who don't know much about MVC think that MVC is a design pattern, in fact they are totally different two things. In short, the framework is often code reuse, while design patterns are reuse, architecture is in between, part of the code reuse, part of the design reuse, and sometimes can analyze reuse.


Quoting a sentence, the framework is a great wisdom, used for the division of Software design, design mode is a small skill, to solve specific problems, to improve code reuse rate, reduce coupling degree

MVC just one of the frameworks, there are many other common frameworks, such as: Struts , Spring , Zend Framework as well . NET MVC and so on, looking forward to learning and using them.

The MVC framework for Web development

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.