ASP. NET MVC has been launched for a long time, and many projects are already using this excellent web development framework. Because every time we add people to the project, MVC is not particularly familiar, some people think this is very simple, resulting in the program is not MVC, so I would like to write a series summarizing the actual combat experience and some study notes. Let's not talk about the benefits of MVC until we have written some programs and examples and come back to talk.
First, let's use MVC to write a simple ASP. NET MVC3 program to get a simple look.
Environment:
VS2010
ASP. MVC3
In http://www.microsoft.com/web/downloads/platform.aspx download Microsoft Web Platform Installer 3.0, in Microsoft Web platform Installer 3.0 can be downloaded online to the required software.
Sample Program
1. Display a simple string "Hello world"
New ASP. MVC3 WEB Application
Select the Razor view engine
Press F5 to start the application
We see the error, this is because in MVC, the request is handled by the controller, and now there is no controller, below we add a controller, right-click Controller to add a HomeController
Change the index method in HomeController to the following
F5 after the run is shown below
Why it is possible to run successfully, thanks to the routing mechanism of ASP. Let's take a look at the following code in Global.asax.cs
From this line of code, you can see that the system default request is routed to the action of HomeController and index (the difference between the two is that the public method of the controller is called Action in MVC).
2. Display a view:
If you write the string directly to the client as you did just now, the developer must be crazy, and now we're going to show a view. First change the action in HomeController to the following code, press F5
1234567891011121314 |
namespace HelloWorld.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
}
}
|
According to the prompt, we add the corresponding file in any location of the error prompt, here we add index.aspx under the Views/home folder, right click on the index method name in the HomeController, select the Add View
Click OK to change index.aspx to the following:
F5 run, we see the same results as shown before
3. Add Dynamic Content
Add the following code
Modify the Index.aspx view as follows
F5 Run
ViewBag is a property of a dynamic type defined by the controller, which means that you can add any property to him, and the properties of the dynamic type are not checked at compile time.
4. Strongly typed views
Dynamic type compilation can not be checked, when development is not only prompt is very uncomfortable when the comments, we define a strongly typed view. Define a person class first
Delete index.aspx, ctrl+shift+b compile, right-click Action->add View
Modify Index.aspx
We can see the strong type of smart tips
F5 Run
We can see the generated HTML and page
Add the following method to the controller
The submission form appears as follows,
The name is displayed because ASP. NET MVC uses the binder mechanism to assign the data of the form post to the properties of the Parameter object, and we can look at the data of the form post by Firebug
5. Add validation
Modify person This modal as follows, remember to reference the Red label namespace
In the view, add the following red callout
Modify the code in the action as follows
F5 run, do not enter any code to submit the form
So far, we have completed a simple MVC3 program that demonstrates some of the basic features of the MVC3.
Reprint: http://www.cnblogs.com/cnblogsfans/archive/2011/09/01/2162227.html
ASP. NET MVC3 Combat Series (i): a simple example