MVC Technology Exchange, welcome Dabigatran: This article aims
1. Understand the process of creating, debugging, and using the model, view, controller.
Directory of this document
1. Create a model
2. Create a View
3. Create a Controller
4. Commissioning
5. Using models, views, controllers
1. Create a model
Create a new class in the folder "Models" as shown in:
1, naming: Model naming rules generally end with the model, such as: business-oriented named Usermodel or page-based naming Loginmodel.
2, the role: the development of three layers of friends know that we are in the presentation layer, business logic layer, data manipulation layer when the use of the DTO will be used to create a unique name of the class library project known as model. In fact, the class in the Models folder here is essentially the same as the data transfer object we used to use. Of course there will be some differences, we'll talk about it later.
3. code example:
1/* 2 * 3 * Creator: Li Lin 4 * 5 * Time: 2013-2-24 6 * 7 * Description: User Model 8 * 9 */10 one using system;12 namespace MVC3. DEMO.MODELS14 {15//business-oriented user mode-public class UserModel17 {$ public int UserID {get; set;} User number: public string UserName {get; set;} User name: public string Password {get; set;} Password: public int Sex {get; set;} Sex, 0 male, 1 female public int-age {get; set;} Age of public int political {get; set;} Political appearance of public int Height {get; set;} Height: public int Weight {get; set;} Weight of public string graduated {get; set;} University of the graduated public string Professional {get; set;} Professional-Public DateTime graduateddate {get; set;} Graduation Date: public string Address {get; set;} Live address of public string Phone { Get Set }//Contact Phone: public string ImagePath {get; set;} Header address of the public string other {get; set;} Other Description 33}34 35//page-based user login model of public class LoginModel37 {$ public string UserName {get; set; }//User name All public string Password {get; set;} Password 40}41//page-based user registration mode of public class RegeditModel43 {$ public string UserName {get; set;} User name: public string Password {get; set;} Password: public int Sex {get; set;} Gender-public INT-age {get; set;} Age 48}49}
2. Create a View
Before creating a view, we will create a folder called User, which creates a view in the User folder ("Add" and "View") as shown in:
1, Name: The same as the previous name, the function of the main page, here is: Login.
2, Template engine: here to choose razor (reason please see my previous article).
3. Create a strongly-typed view: The check box here will automatically list the models we have built (note: This is only the build after the build, and sometimes just after the creation of the model here is not, build can appear later), as we have just built the three model will be shown here. The difference between tick and uncheck is that there is no code associated with the model in the created view file, which I will comment on in the code block.
4, Frame Template: Only select the Model class after this item is optional, which has "create, delete, detailed, edit, empty, List" selection, when one of the selected template VS2010 will automatically generate the corresponding model of the Operation code. This feature is actually a View code generator for VS2010.
5. Create part of the page: As with the traditional ASPX page UserControl, you can create the app in a different view at once.
6. Select a master page: Select whether to select a master page
7. code example:
1 @model MVC3. Demo.Models.LoginModel 2 <!--If you create a strongly typed view and select Loginmodel, the above code is automatically generated by VS, just a piece of code, the handwriting effect is the same, so generally I will not check the "strongly typed view"-- > 3 @{4 layout = null; 5} 6 <!--layouts are used for the placement of the master page in the fourth article in this series, which will be explained in detail--7 <! DOCTYPE html> 8 3. Create a ControllerCreate "Usercontroller" in the control folder, as shown in:
1. Name: Must end with controller.
2. Templates: Three of them are optional, "empty, use EF, direct read and write", function similar to code generator, select VS2010 Automatically Add method code block in the created class.
3. code example:
1/* 2 * 3 * Creator: Li Lin 4 * 5 * Time : 2013-2-24 6 * 7 * Description : User Controller 8 * 9 */10 11 Using system;12 using system.collections.generic;13 using system.web;14 using System.web.mvc;15 namespace MVC3. DEMO.CONTROLLERS17 {public class Usercontroller:controller19 { ////system automatically generated, you can delete the Public ActionResult Index () //{23 // return View (), //}25 //Login Controller ActionResult Login () { return View (); }31 }32}
4. UserControl contains a method Login executes the views/user/login.schtml and returns.
4. Commissioning1. Debugging: Unlike traditional ASPX pages, MVC cannot be browsed directly with the right mouse button. Debug MVC to be in "MVC3." Right-click on the Properties "Settings Start page as shown in the demo project:
2, Run effect: Press F5 to run, plus breakpoints as shown in the traditional ASPX page.
5. Using models, views, controllers1. Modify the Logincontrol code as follows:
1/* 2 * 3 * Creator: Li Lin 4 * 5 * Time : 2013-2-24 6 * 7 * Description : User Controller 8 * 9 */10 one us ing system;12 using system.collections.generic;13 using system.web;14 using System.web.mvc;15 namespace MVC3. DEMO.CONTROLLERS17 {public class Usercontroller:controller19 { ////system automatically generated, you can delete the Public ActionResult Index () //{23 // return View (), //}25 //Login Controller ActionResult Login () { return View (); }31 [httppost]//Login Controller ActionResult Login (Models.loginmodel loginmodel) ( loginmodel.username = = "Zhang San" && Loginmodel.password = = "123456") Response.Write ("Correct!") "); PNs else38 Response.Write (" Incorrect! "); }41 }42}
There are two methods of the same name in the Logincontrol login, no parameter is when the user opens the login window through the Address bar method, adding [HttpPost] method is the user click on the submission when the postback process.
2. Mvc execution Flowchart (rough):
3, this example execution process:
- The user enters Http://localhost/User/Login in the address bar
- Server-side routing resolves addresses
- Find the Login method for Usercontorl in Contorls (if a postback executes a method with [HttpPost])
- Execution logic, where the user name and password are determined
- Return to the corresponding view
Copyright: Http://www.cnblogs.com/iamlilinfeng
No crap mvc Getting Started tutorial two [first small demo]