276Microsoft Visual Studio 2010: A Beginner’s Guide

In the HTTP protocol, there are different types of verbs for the operation being conducted. Listing 9-9 demonstrates two of these verbs, get and post. A get is typically associated with reading data, and a post is typically associated with writing data. Listing 9-9 shows both get and post methods in the Create method overloads. In ASP.NET MVC, action methods default to get requests and you must use an HttpVerbs attribute to specify a post.

The get Create action method instantiates a new Customer object and passes it to the View. When the user fills in the form and submits, the post Create action method will execute and insert the new record into the database.

Notice how I changed the Create method parameter from FormsCollection to Customer. ASP.NET MVC will automatically read the form values and match those values up with matching properties in the object passed to the method. The method also checks to ensure that the name is filled in and adds an error to the ModelState. Whenever an error occurs, you need to return to the same View to ensure the user sees the error and can correct and resubmit. ASP.NET MVC will use this error to display error messages in the View. To create the View, right-click either Create method, select Add View, and fill in the values as shown in Figure 9-7.

Figure 9-7 Adding a new Customer

Page 299
Image 299
Microsoft 9GD00001 manual Adding a new Customer