29.05.2015 Views

o_19mgorv9t13a3ko71fev19l81mqa.pdf

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

shopping<br />

<br />

I added a new column to each row of the table that contains a form with an input element. I styled the input element<br />

as a button with Bootstrap and added a style element and an id to the table element to ensure that the button and the<br />

content of the other columns are properly aligned.<br />

Note I used the strongly typed Html.HiddenFor helper method to create a hidden field for the ReturnUrl model<br />

property, but I had to use the string-based Html.Hidden helper to do the same for the ProductId field. If I had written<br />

Html.HiddenFor(x => line.Product.ProductID), the helper would render a hidden field with the name<br />

line.Product.ProductID. The name of the field would not match the names of the parameters for the<br />

CartController.RemoveFromCart action method, which would prevent the default model binders from working,<br />

so the MVC Framework would not be able to call the method.<br />

You can see the Remove buttons at work by running the application and adding items to the shopping cart. Remember that<br />

the cart already contains the functionality to remove it, which you can test by clicking one of the new buttons, as shown in Figure<br />

9-1.<br />

Figure 9-1. Removing an item from the shopping cart<br />

Adding the Cart Summary<br />

I may have a functioning cart, but there is an issue with the way it is integrated into the interface. Customers can tell what is in<br />

their cart only by viewing the cart summary screen. And they can view the cart summary screen only by adding a new a new item<br />

to the cart.<br />

To solve this problem, I am going to add a widget that summarizes the contents of the cart and that can be clicked to display<br />

the cart contents throughout the application. I will do this in much the same way that I added the navigation widget—as an action<br />

whose output I will inject into the Razor layout. To start, I need to add the simple method, shown in Listing 9-5, to the<br />

CartController class.<br />

234

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!