Part 9: Registration and Checkout
by Jon Galloway
The MVC Music Store is a tutorial application that introduces and explains step-by-step how to use ASP.NET MVC and Visual Studio for web development.
The MVC Music Store is a lightweight sample store implementation which sells music albums online, and implements basic site administration, user sign-in, and shopping cart functionality.
This tutorial series details all of the steps taken to build the ASP.NET MVC Music Store sample application. Part 9 covers Registration and Checkout.
In this section, we will be creating a CheckoutController which will collect the shopper’s address and payment information. We will require users to register with our site prior to checking out, so this controller will require authorization.
Users will navigate to the checkout process from their shopping cart by clicking the “Checkout” button.
If the user is not logged in, they will be prompted to.
Upon successful login, the user is then shown the Address and Payment view.
Once they have filled the form and submitted the order, they will be shown the order confirmation screen.
Attempting to view either a non-existent order or an order that doesn’t belong to you will show the Error view.
Migrating the Shopping Cart
While the shopping process is anonymous, when the user clicks on the Checkout button, they will be required to register and login. Users will expect that we will maintain their shopping cart information between visits, so we will need to associate the shopping cart information with a user when they complete registration or login.
This is actually very simple to do, as our ShoppingCart class already has a method which will associate all the items in the current cart with a username. We will just need to call this method when a user completes registration or login.
Open the AccountController class that we added when we were setting up Membership and Authorization. Add a using statement referencing MvcMusicStore.Models, then add the following MigrateShoppingCart method:
[!code-csharpMain]
1: private void MigrateShoppingCart(string UserName)
2: {
3: // Associate shopping cart items with logged-in user
4: var cart = ShoppingCart.GetCart(this.HttpContext);
5:
6: cart.MigrateCart(UserName);
7: Session[ShoppingCart.CartSessionKey] = UserName;
8: }
Next, modify the LogOn post action to call MigrateShoppingCart after the user has been validated, as shown below:
[!code-csharpMain]
1: //
2: // POST: /Account/LogOn
3: [HttpPost]
4: public ActionResult LogOn(LogOnModel model, string returnUrl)
5: {
6: if (ModelState.IsValid)
7: {
8: if (Membership.ValidateUser(model.UserName, model.Password))
9: {
10: MigrateShoppingCart(model.UserName);
11:
12: FormsAuthentication.SetAuthCookie(model.UserName,
13: model.RememberMe);
14: if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1
15: && returnUrl.StartsWith("/")
16: && !returnUrl.StartsWith("//") &&
17: !returnUrl.StartsWith("/\\"))
18: {
19: return Redirect(returnUrl);
20: }
21: else
22: {
23: return RedirectToAction("Index", "Home");
24: }
25: }
26: else
27: {
28: ModelState.AddModelError("", "The user name or password provided is incorrect.");
29: }
30: }
31: // If we got this far, something failed, redisplay form
32: return View(model);
33: }
Make the same change to the Register post action, immediately after the user account is successfully created:
[!code-csharpMain]
1: //
2: // POST: /Account/Register
3: [HttpPost]
4: public ActionResult Register(RegisterModel model)
5: {
6: if (ModelState.IsValid)
7: {
8: // Attempt to register the user
9: MembershipCreateStatus createStatus;
10: Membership.CreateUser(model.UserName, model.Password, model.Email,
11: "question", "answer", true, null, out
12: createStatus);
13:
14: if (createStatus == MembershipCreateStatus.Success)
15: {
16: MigrateShoppingCart(model.UserName);
17:
18: FormsAuthentication.SetAuthCookie(model.UserName, false /*
19: createPersistentCookie */);
20: return RedirectToAction("Index", "Home");
21: }
22: else
23: {
24: ModelState.AddModelError("", ErrorCodeToString(createStatus));
25: }
26: }
27: // If we got this far, something failed, redisplay form
28: return View(model);
29: }
That’s it - now an anonymous shopping cart will be automatically transferred to a user account upon successful registration or login.
Creating the CheckoutController
Right-click on the Controllers folder and add a new Controller to the project named CheckoutController using the Empty controller template.
First, add the Authorize attribute above the Controller class declaration to require users to register before checkout:
[!code-csharpMain]
1: namespace MvcMusicStore.Controllers
2: {
3: [Authorize]
4: public class CheckoutController : Controller
Note: This is similar to the change we previously made to the StoreManagerController, but in that case the Authorize attribute required that the user be in an Administrator role. In the Checkout Controller, we’re requiring the user be logged in but aren’t requiring that they be administrators.
For the sake of simplicity, we won’t be dealing with payment information in this tutorial. Instead, we are allowing users to check out using a promotional code. We will store this promotional code using a constant named PromoCode.
As in the StoreController, we’ll declare a field to hold an instance of the MusicStoreEntities class, named storeDB. In order to make use of the MusicStoreEntities class, we will need to add a using statement for the MvcMusicStore.Models namespace. The top of our Checkout controller appears below.
[!code-csharpMain]
1: using System;
2: using System.Linq;
3: using System.Web.Mvc;
4: using MvcMusicStore.Models;
5:
6: namespace MvcMusicStore.Controllers
7: {
8: [Authorize]
9: public class CheckoutController : Controller
10: {
11: MusicStoreEntities storeDB = new MusicStoreEntities();
12: const string PromoCode = "FREE";
The CheckoutController will have the following controller actions:
AddressAndPayment (GET method) will display a form to allow the user to enter their information.
AddressAndPayment (POST method) will validate the input and process the order.
Complete will be shown after a user has successfully finished the checkout process. This view will include the user’s order number, as confirmation.
First, let’s rename the Index controller action (which was generated when we created the controller) to AddressAndPayment. This controller action just displays the checkout form, so it doesn’t require any model information.
[!code-csharpMain]
1: //
2: // GET: /Checkout/AddressAndPayment
3: public ActionResult AddressAndPayment()
4: {
5: return View();
6: }
Our AddressAndPayment POST method will follow the same pattern we used in the StoreManagerController: it will try to accept the form submission and complete the order, and will re-display the form if it fails.
After validating the form input meets our validation requirements for an Order, we will check the PromoCode form value directly. Assuming everything is correct, we will save the updated information with the order, tell the ShoppingCart object to complete the order process, and redirect to the Complete action.
[!code-csharpMain]
1: //
2: // POST: /Checkout/AddressAndPayment
3: [HttpPost]
4: public ActionResult AddressAndPayment(FormCollection values)
5: {
6: var order = new Order();
7: TryUpdateModel(order);
8:
9: try
10: {
11: if (string.Equals(values["PromoCode"], PromoCode,
12: StringComparison.OrdinalIgnoreCase) == false)
13: {
14: return View(order);
15: }
16: else
17: {
18: order.Username = User.Identity.Name;
19: order.OrderDate = DateTime.Now;
20:
21: //Save Order
22: storeDB.Orders.Add(order);
23: storeDB.SaveChanges();
24: //Process the order
25: var cart = ShoppingCart.GetCart(this.HttpContext);
26: cart.CreateOrder(order);
27:
28: return RedirectToAction("Complete",
29: new { id = order.OrderId });
30: }
31: }
32: catch
33: {
34: //Invalid - redisplay with errors
35: return View(order);
36: }
37: }
Upon successful completion of the checkout process, users will be redirected to the Complete controller action. This action will perform a simple check to validate that the order does indeed belong to the logged-in user before showing the order number as a confirmation.
[!code-csharpMain]
1: //
2: // GET: /Checkout/Complete
3: public ActionResult Complete(int id)
4: {
5: // Validate customer owns this order
6: bool isValid = storeDB.Orders.Any(
7: o => o.OrderId == id &&
8: o.Username == User.Identity.Name);
9:
10: if (isValid)
11: {
12: return View(id);
13: }
14: else
15: {
16: return View("Error");
17: }
18: }
Note: The Error view was automatically created for us in the /Views/Shared folder when we began the project.
The complete CheckoutController code is as follows:
[!code-csharpMain]
1: using System;
2: using System.Linq;
3: using System.Web.Mvc;
4: using MvcMusicStore.Models;
5:
6: namespace MvcMusicStore.Controllers
7: {
8: [Authorize]
9: public class CheckoutController : Controller
10: {
11: MusicStoreEntities storeDB = new MusicStoreEntities();
12: const string PromoCode = "FREE";
13: //
14: // GET: /Checkout/AddressAndPayment
15: public ActionResult AddressAndPayment()
16: {
17: return View();
18: }
19: //
20: // POST: /Checkout/AddressAndPayment
21: [HttpPost]
22: public ActionResult AddressAndPayment(FormCollection values)
23: {
24: var order = new Order();
25: TryUpdateModel(order);
26:
27: try
28: {
29: if (string.Equals(values["PromoCode"], PromoCode,
30: StringComparison.OrdinalIgnoreCase) == false)
31: {
32: return View(order);
33: }
34: else
35: {
36: order.Username = User.Identity.Name;
37: order.OrderDate = DateTime.Now;
38:
39: //Save Order
40: storeDB.Orders.Add(order);
41: storeDB.SaveChanges();
42: //Process the order
43: var cart = ShoppingCart.GetCart(this.HttpContext);
44: cart.CreateOrder(order);
45:
46: return RedirectToAction("Complete",
47: new { id = order.OrderId });
48: }
49: }
50: catch
51: {
52: //Invalid - redisplay with errors
53: return View(order);
54: }
55: }
56: //
57: // GET: /Checkout/Complete
58: public ActionResult Complete(int id)
59: {
60: // Validate customer owns this order
61: bool isValid = storeDB.Orders.Any(
62: o => o.OrderId == id &&
63: o.Username == User.Identity.Name);
64:
65: if (isValid)
66: {
67: return View(id);
68: }
69: else
70: {
71: return View("Error");
72: }
73: }
74: }
75: }
Adding the AddressAndPayment view
Now, let’s create the AddressAndPayment view. Right-click on one of the the AddressAndPayment controller actions and add a view named AddressAndPayment which is strongly typed as an Order and uses the Edit template, as shown below.
This view will make use of two of the techniques we looked at while building the StoreManagerEdit view:
- We will use Html.EditorForModel() to display form fields for the Order model
- We will leverage validation rules using an Order class with validation attributes
We’ll start by updating the form code to use Html.EditorForModel(), followed by an additional textbox for the Promo Code. The complete code for the AddressAndPayment view is shown below.
[!code-cshtmlMain]
1: @model MvcMusicStore.Models.Order
2: @{
3: ViewBag.Title = "Address And Payment";
4: }
5: <script src="@Url.Content("~/Scripts/jquery.validate.min.js")"
6: type="text/javascript"></script>
7: <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"
8: type="text/javascript"></script>
9: @using (Html.BeginForm()) {
10:
11: <h2>Address And Payment</h2>
12: <fieldset>
13: <legend>Shipping Information</legend>
14: @Html.EditorForModel()
15: </fieldset>
16: <fieldset>
17: <legend>Payment</legend>
18: <p>We're running a promotion: all music is free
19: with the promo code: "FREE"</p>
20: <div class="editor-label">
21: @Html.Label("Promo Code")
22: </div>
23: <div class="editor-field">
24: @Html.TextBox("PromoCode")
25: </div>
26: </fieldset>
27:
28: <input type="submit" value="Submit Order" />
29: }
Defining validation rules for the Order
Now that our view is set up, we will set up the validation rules for our Order model as we did previously for the Album model. Right-click on the Models folder and add a class named Order. In addition to the validation attributes we used previously for the Album, we will also be using a Regular Expression to validate the user’s e-mail address.
[!code-csharpMain]
1: using System.Collections.Generic;
2: using System.ComponentModel;
3: using System.ComponentModel.DataAnnotations;
4: using System.Web.Mvc;
5:
6: namespace MvcMusicStore.Models
7: {
8: [Bind(Exclude = "OrderId")]
9: public partial class Order
10: {
11: [ScaffoldColumn(false)]
12: public int OrderId { get; set; }
13: [ScaffoldColumn(false)]
14: public System.DateTime OrderDate { get; set; }
15: [ScaffoldColumn(false)]
16: public string Username { get; set; }
17: [Required(ErrorMessage = "First Name is required")]
18: [DisplayName("First Name")]
19: [StringLength(160)]
20: public string FirstName { get; set; }
21: [Required(ErrorMessage = "Last Name is required")]
22: [DisplayName("Last Name")]
23: [StringLength(160)]
24: public string LastName { get; set; }
25: [Required(ErrorMessage = "Address is required")]
26: [StringLength(70)]
27: public string Address { get; set; }
28: [Required(ErrorMessage = "City is required")]
29: [StringLength(40)]
30: public string City { get; set; }
31: [Required(ErrorMessage = "State is required")]
32: [StringLength(40)]
33: public string State { get; set; }
34: [Required(ErrorMessage = "Postal Code is required")]
35: [DisplayName("Postal Code")]
36: [StringLength(10)]
37: public string PostalCode { get; set; }
38: [Required(ErrorMessage = "Country is required")]
39: [StringLength(40)]
40: public string Country { get; set; }
41: [Required(ErrorMessage = "Phone is required")]
42: [StringLength(24)]
43: public string Phone { get; set; }
44: [Required(ErrorMessage = "Email Address is required")]
45: [DisplayName("Email Address")]
46:
47: [RegularExpression(@"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}",
48: ErrorMessage = "Email is is not valid.")]
49: [DataType(DataType.EmailAddress)]
50: public string Email { get; set; }
51: [ScaffoldColumn(false)]
52: public decimal Total { get; set; }
53: public List<OrderDetail> OrderDetails { get; set; }
54: }
55: }
Attempting to submit the form with missing or invalid information will now show error message using client-side validation.
Okay, we’ve done most of the hard work for the checkout process; we just have a few odds and ends to finish. We need to add two simple views, and we need to take care of the handoff of the cart information during the login process.
Adding the Checkout Complete view
The Checkout Complete view is pretty simple, as it just needs to display the Order ID. Right-click on the Complete controller action and add a view named Complete which is strongly typed as an int.
Now we will update the view code to display the Order ID, as shown below.
[!code-cshtmlMain]
1: @model int
2: @{
3: ViewBag.Title = "Checkout Complete";
4: }
5: <h2>Checkout Complete</h2>
6: <p>Thanks for your order! Your order number is: @Model</p>
7: <p>How about shopping for some more music in our
8: @Html.ActionLink("store",
9: "Index", "Home")
10: </p>
Updating The Error view
The default template includes an Error view in the Shared views folder so that it can be re-used elsewhere in the site. This Error view contains a very simple error and doesn’t use our site Layout, so we’ll update it.
Since this is a generic error page, the content is very simple. We’ll include a message and a link to navigate to the previous page in history if the user wants to re-try their action.
[!code-cshtmlMain]
1: @{
2: ViewBag.Title = "Error";
3: }
4:
5: <h2>Error</h2>
6:
7: <p>We're sorry, we've hit an unexpected error.
8: <a href="javascript:history.go(-1)">Click here</a>
9: if you'd like to go back and try that again.</p>
|