Getting started with MVC instances and getting started with mvc

Source: Internet
Author: User

Getting started with MVC instances and getting started with mvc

The full name of MVC is Model View Controller, short for model-view-controller. It is a Model of software design, organize Code by means of separation of business logic, data, and interface display, and integrate the business logic into a component to improve and personalize the custom interface and user interaction, you do not need to rewrite the business logic. MVC is uniquely developed to map traditional input, processing, and output functions in a logical graphical user interface structure.


Quick Start. Today, we are going to create an mvc project from scratch to introduce relevant knowledge points. Let's get started. The simple demo I am doing is the most basic, and different options will be made based on the specific business in a specific project. But it does not affect our understanding of the principles and processes.

Demo uses the MVC small project to display text on the interface.


1. Add controller

Open VS to create a project. Mvc4 I selected



This is what it looks like after it is created. 1. We can see that it automatically generates the Models Views Controllers three folders. That is, MVC.


Next we will create a controller. Right-click controllers and add the -- controller.

Note that the suffix controller cannot be changed. You can change the selected blue part as needed.


Knowledge point: the newly created controller class is different from the common class. It inherits the controller. When we view the controller, we find that its physical path is in the folder of the newly created project. That is to say, this is automatically added and has been automatically referenced.



2. An Action method is automatically generated at the same time. It is used to process businesses and operate databases.

Second, create a class code in the model as follows.


3. Add view

Right-click the defaultcontroller class you created in the code. Add a view. The default view name is the same as the controller class name.


Knowledge Point: After the view is created, you will find that the view is automatically added to the views folder. Actions in a controller can correspond to a view.


Code 4

With the new class controller model views, add the following code to the controller.

The point is how I transfer the value in the controller to the view.

① Use ViewBag to get the content to be passed in the controller.

Using MvcDemo. models; 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> /// initialize the data set /// </summary> public List <Models. apple> InitData () {List <Models. apple> list = new List <Models. apple> () {new 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 (4000); // process the current business, for example, read database judgment, etc. // create a data set (pseudo data) List <Models. apple> list = InitData (); // traverses the collection to generate HTML code and store it to sbHtml list. forEach (d => {sbHtml. appendLine ("<div>" + d. toString () + "</div> ");}); // use ViewBag to transmit to the index cshtml view with the same name // viewBag is a set of dynamic types. You can dynamically add attributes and values of any type of names to ViewBag. htmlStr = sbHtml. toString (); return View ();}}}


② Add the following statement to view. You can.

@ {Layout = null ;}<! DOCTYPE html> 


In this case, right-click Index and right-click page inspector to view the effect.


Conclusion: You need to repeat the code and think about it. Think twice without thinking, and think twice without thinking.

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.