Annotating Orders for Validation

A user who tries to purchase music from the ASP.NET MVC Music Store will go through a typical shopping cart checkout procedure. The procedure requires payment and shipping information. The Order class (presented in the following code), represents everything the application needs to complete a checkout:

public class Order
{
    public int OrderId { get; set; }
    public System.DateTime OrderDate { get; set; }
    public string Username { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string PostalCode { get; set; }
    public string Country { get; set; }
    public string Phone { get; set; }
    public string Email { get; set; }
    public decimal Total { get; set; }
    public List<OrderDetail> OrderDetails { get; set; }
}

Some of the properties in the Order class require user input (such as FirstName and LastName), while the application derives other property values from the environment, or looks them up from the database (such as the Username property, because a user must log in before checking out, thus the application will already have the value).

The application builds the checkout page using the EditorForModel HTML helper. The following code is from the AddressandPayment.cshtml view in the Views/Checkout folder:

<fieldset>
    <legend>Shipping Information</legend>
    @Html.EditorForModel()
</fieldset>

The EditorForModel

Get Professional ASP.NET MVC 3 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.