ASP. NET mvc3 practice series (1): simple example

Source: Internet
Author: User
Tags form post

ASP. net mvc has been released for a short time, and many projects are using this excellent web development framework. This is because every time we add a person to a project, we are not very familiar with MVC. Some people think this is very simple, leading to writtenProgramIt is not MVC at all, so I want to write a series to summarize the practical experience and some learning notes. Let's not talk about the advantages of MVC. We will come back and talk about it after we have written some programs and examples.

First, we will first use MVC to write a simple ASP. NET mvc3 program.

 

Environment:

Vs2010

ASP. NET mvc3

Download Microsoft Web platform installer 3.0 from the http://www.microsoft.com/web/downloads/platform.aspx and download the software you need online on Microsoft Web platform installer 3.0.

 

Sample program

1. display a simple string "Hello World"

Create an ASP. NET mvc3 Web Application

Select razor view Engine

Press F5 to start the application

We can see an error. This is because in MVC, the request is processed by the Controller, and no controller is available now. Next we add a controller and right-click the Controller to add a homecontroller.

Modify the index method in homecontroller as follows:

After F5 is run

The reason for the successful operation is that the Routing Mechanism of ASP. net mvc. Let's take a look at the following in global. asax. CS:Code

From this line of code, we can see that the system's default request is routed to the action homecontroller and index (MVC calls the public method in controller action, and there is a difference between the two ).

2. display a view:

If you write strings directly to the client as you did, the developer will be crazy. Now we will display a view. First, change the action in homecontroller to the following code, and press F5

 
Namespace helloworld. controllers {public class homecontroller: controller {// get:/home/Public actionresult index () {return view ();}}}

 

As prompted, we can add the corresponding file to any location in the error prompt. Here we add the index under the views/home folder. aspx, right-click the index method name in homecontroller, and select Add View

Click OK to modify index. aspx to the following content:

Run F5. We can see the same result as before.

 

3. Add dynamic content

Add the following code:

Modify the index. aspx view as follows:

F5 run

Viewbag is a dynamic attribute defined by the Controller, which means you can add any attribute to it. during compilation, the dynamic attributes are not checked.

 

4. Strong type view

Dynamic type compilation cannot be checked. When there is no prompt during development, it is difficult to comment. Next we will define a strong type view. First define a person class

Delete index. aspx, CTRL + Shift + B, right-click action-> Add View

Modify index. aspx

We can see smart prompts of strong types.

F5 run

We can see the generated HTML and page

Add the following method to controller:

The submission form is shown as follows,

The reason why the name can be displayed is that ASP. net mvc assigns the form post data to the attribute corresponding to the parameter object through the Binder Mechanism. We can look at the form post data through firebug.

 

5. Add Verification

Modify the "person" modal as follows. Remember to reference the namespace marked in red.

Add the following red content to the View:

Modify the code in the action as follows:

Run F5 without entering any code to submit the form

 

So far, we have completed a simple mvc3 program, which demonstrates some basic features of mvc3.

 

Finally, make a small advertisement: the Xi'an branch is eager to hire the following personnel. Interested parties can contact me directly. The left-side bar shows my contact information. The blog Park recruitment channel has a detailed description.Http://job.cnblogs.com/offer/13800/

Basic requirements:

More than 4 years of C # development experience, solid. Net Foundation, and skillful use of. net3.5 new features.

Proficient in ASP. NET web development.

Familiar with WCF.

Familiar with Orm, LINQ to SQL/Entity Framework or nhib.pdf.

Familiar with JavaScript and jquery.

Familiar with web standards and HTML & CSS.

Familiar with SQL Server, familiar with T-SQL, stored procedures.

Proficient in English reading and writing, can understand English requirements, can communicate smoothly with the customer in English (MSN/Skype)

Good coding habits.



Be familiar with any of the following priorities:

Knowledge of agile developers is preferred

Proficiency in English Listening and Speaking is preferred

Familiar with ASP. net mvc is preferred.

Familiar with WPF is preferred

Familiar with Silverlight first

Experience in system architecture is preferred, and experience in unit test or TDD is preferred.

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.