4.在Add View对话框中，从View data class标记的下拉列表框中，选择ToyStore.Models.Product类。
5.在Add View对话框中，从View Content标记的下拉列表框中，选择List。
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:
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") %>
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 ***
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.