ASP. net mvc simple programming (1)

Source: Internet
Author: User

 

ASP. net mvc concept:

ASP. net mvc is an official MVC model provided by Microsoft. To put it bluntly, ASP. net mvc knowledge development ASP. NET Web

It is only a presentation layer framework and the first open-source project of Microsoft.

Using ASP. NET to develop web Applications

  1. WebForm
  2. Asp. Net MVC

Note: WebForm and Asp. net MVC is two parallel methods for developing web applications using asp.net. The purpose of mvc (hereinafter referred to as mvc) is not to replace WebForm, but to be another choice for web development.

Introduction to the MVC mode:

The MVC mode is a manifestation mode. It divides web applications into three main components: View, Controller, and Model ).

M: Model is mainly used to store or process data.

In fact, the Model implements operations on the database corresponding to the entity class at the business logic layer, such as CRUD (Create/Retrieve/Update/Delete ). It includes application information such as data, verification rules, data access, and business logic.

V: View is a user interface layer component. It mainly displays the data in the Model to the user.

C: The Controller processes user interaction, obtains data from the Model, and transmits the data to the specified View.

Key points:

01. M and V implement code separation, so that the same program can use different forms of representation. The purpose of C is to ensure synchronization between M and V. Once M changes, V should be updated synchronously.

02. The controller isolates the coupling between the business logic and the View, so that the business logic and data rules can be easily changed without affecting the front-end page.

03. Both View and Controller can directly request the Model, but the Model does not depend on View and Controller.

04. The Controller can directly request the View to display specific pages. The View does not depend on the Controller.

The first Asp. Net MVC Program

Case: The first Asp. Net MVC application is written by adding data to the Customer table of the EFFirst database and displaying all user information on the corresponding page.

Customer table structure:

CustId (customer ID): The primary key automatically increases

CustName (customer name)

CustAddress)

01. Create a MVC2.0 project first

Right-click the solution and create a project, as shown in figure

 

Name the project MyMVC and click OK, as shown in figure

 

Select No and click OK.

 

Next, we will analyze the directory structure of the project:

App_Data; files in this folder are accessed through different URLs to protect files

Content: The materials and styles used by the project. For example, you can put the Image folder and Css folder into the Content folder.

Controllers: controller storage folder

Models: model storage folder

Scripts: js (javascript) file storage folder

Views: View storage folder

 

02. Create a controller UserInfoController

03. Create a view Register

The Register page initiates a request to the Controller. There are two types of requests: get and post. Therefore, we write two actions. To facilitate subsequent operations, we can write an overload for the action. Register () without parameters is used to process get requests, and the Register (FormCollection collection) with parameters is used to process post requests, such:

 

Right-click "Register" without parameters and select "add View menu item ".

Click the Add button. A folder named UserInfo (the same as the controller name of the action) is automatically created in the Views folder, create An aspx file named Register in this folder. Note: The aspx file is not suffixed. cs files.

Write the html code in Register as follows:

 

Since the registration page is used, you must submit the information entered by the user to the server. In this case, you can use the form. Set the property value to "/UserInfo/Register" for its action attribute. UserInfo indicates UserInfoController and Register indicates action under UserInfoController. That is to say, we submit the information to the Register action.

After the user fills in the information, the process jumps to UserInfoController. At this time, we can write the following code in the action that receives the Post request,

 

Note: return RedirectToAction ("Index") is used to jump to the action named Index to continue execution.

04. Create action: Index and view Index

 

Right-click an Index in the same way to create a View: Index,

1 <style type = "text/css"> 2 th, td {3 text-align: center; 4} 5 </style> 6 

View Code

 

 

Finally, we can achieve the following results:

So far, today's article is over.

Currently write the first MVC application also used EF knowledge, if the reader is not very clear, you can refer to my http://www.cnblogs.com/weilengdeyu/archive/2013/01/19/2868022.html

 

Related Article

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.