An MVC view contains all of the HTML markup and view logic required to generate an HTML page. The set of views exposed by an ASP.NET MVC application is the public face of the application.
*** Begin Note ***
A view does not need to be HTML. For example, you can create Silverlight views.
*** End Note ***
Our simple application needs two views: the Index and the Create view. We’ll use the Index view to displays the list of products and the Create view to display a form. for creating new products.
Adding the Index View
Let’s start by creating the Index view. Follow these steps:
1. Build your application by selecting the menu option Build, Build Solution.
2. Right-click the Index() action in the Code editor window and select the menu option Add View (see Figure 11).
3. In the Add View dialog, select the option Create a strongly-typed view.
4. In the Add View dialog, from the dropdown list labeled View data class, select the ToyStore.Models.Product class.
5. In the Add View dialog, from the dropdown list labeled View Content, select List.
6. Click the Add button to add the new view to your project (see Figure 12).
4.在Add View对话框中,从View data class标记的下拉列表框中,选择ToyStore.Models.Product类。
5.在Add View对话框中,从View Content标记的下拉列表框中,选择List。
6.点击Add按钮将这个新视图添加到您的项目里(见图12)。
Figure 11 – Adding a view
Figure 12 – The Add View dialog
*** Begin Note ***
You need to build your ASP.NET MVC application before adding a view with the Add View dialog in order to build the classes displayed by the View data class dropdown list. If your application has build errors then this list will be blank.
*** End Note ***
Views are added to the Views folder. Views follow a particular naming convention. A view returned by the Index() action exposed by the Home controller class is located at the following path:
\Views\Home\Index.aspx
In general, views follow the naming convention:
\Views\Controller Name\Action Name.aspx
The contents of the Index view are contained in Listing 3. This view loops through all of the products and displays the products in an HTML table (see Figure 13).
【注意】您需要生成您的ASP.NET MVC应用程序后,才能够使用Add View对话框添加一个视图,生成应用程序后,View data class下拉列表框中才会显示您的数据类。如果您在生成应用时发生错误,下拉列表将是空白的。
45. <%=Html.ActionLink("Back to List", "Index") %>
46.
47.
48.
The Create view displays an HTML form. for creating new products (see Figure 15). The Add View dialog generates HTML form. fields that correspond to each of the properties of the Product class. If you complete the HTML form. and submit it, a new product will be created in the database.
*** Begin Warning ***
Our Create form. does not perform. any validation. For example, if you enter the string “apple” for the Price field, the new product will be created in the database with the value 0 in its Price column.
*** End Warning ***
Summary
In this chapter, we used the ASP.NET MVC framework to build a simple database-driven web application. We created models, views, and controllers.
First, we created a database and a database model. We used Microsoft SQL Server Express for our database. We created our database model classes by taking advantage of the Microsoft Entity Framework.
Next, we created the Home controller. We used Visual Studio to generate the actions for our Home controller automatically. We added a few lines of data access logic to interact with our database.
Finally, we created two views. We created an Index view that displays a list of all of the products in an HTML table. We also added a Create view that displays an HTML form. for adding a new product to the database.