O'Reilly logo

Professional ASP.NET MVC 4 by Scott Hanselman, K. Scott Allen, Brad Wilson, Phil Haack, Jon Galloway

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 represents everything the application needs to complete a checkout:

public class Order
{
   public int OrderId { get; set; }
   public 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 helper builds out editors for every property in a ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required