Entity namespace so that you have access to all the core functionality of the Entity Framework. This functionality includes the capability to query, insert, update, and delete data by working with strongly typed objects. The ProductContext class adds access to the newly added CartItem model class. Next, you'll create the ShoppingCart class in a new Logic folder. The ShoppingCart class handles data access to the CartItem table.
The class will also include the business logic to add, remove, and update items in the shopping cart. The shopping cart logic that you will add will contain the functionality to manage the following actions:.
A shopping cart page ShoppingCart. The shopping cart page will display all the items the user adds to the shopping cart. Besides the shopping cart page and class, you'll create a page AddToCart. You will also add code to the ProductList. The following diagram shows the basic process that occurs when the user adds a product to the shopping cart.
The AddToCart. The ShoppingCart. The ShoppingCart class will be added to a separate folder in the application so that there will be a clear distinction between the model Models folder , the pages root folder and the logic Logic folder. Name the new folder Logic. The AddToCart method enables individual products to be included in the shopping cart based on the product ID.
The product is added to the cart, or if the cart already contains an item for that product, the quantity is incremented. The cart ID is used to track the items that a user has in their shopping cart. If the user does not have an existing cart ID , a new cart ID is created for them. If the user is signed in as a registered user, the cart ID is set to their user name. A GUID ensures that only one cart is created for each user, based on session. The GetCartItems method returns a list of shopping cart items for the user.
Later in this tutorial, you will see that model binding is used to display the cart items in the shopping cart using the GetCartItems method. As mentioned earlier, you will create a processing page named AddToCart.
You will be modifying the code-behind AddToCart. When the AddToCart. Next, an instance of the shopping cart class is created and used to call the AddToCart method that you added earlier in this tutorial. If the product hasn't been added to the shopping cart, the product is added to the CartItem table of the database. If the product has already been added to the shopping cart and the user adds an additional item of the same product, the product quantity is incremented in the CartItem table.
Finally, the page redirects back to the ShoppingCart. As previously mentioned, a user ID is used to identify the products that are associated with a specific user.
This ID is added to a row in the CartItem table each time the user adds a product to the shopping cart. It will also provide the ability to add, remove and update items in the shopping cart. Name the new page ShoppingCart.
Select Site. Master to attach the master page to the newly created. This control uses model binding to bind the shopping cart data from the database to the GridView control.
When you set the ItemType property of the GridView control, the data-binding expression Item is available in the markup of the control and the control becomes strongly typed. As mentioned earlier in this tutorial series, you can select details of the Item object using IntelliSense. To configure a data control to use model binding to select data, you set the SelectMethod property of the control.
The GridView data control calls the method at the appropriate time in the page life cycle and automatically binds the returned data. The GetShoppingCartItems method must still be added. Next, you add code to the ShoppingCart. In Solution Explorer , right-click the ShoppingCart. As mentioned above, the GridView data control calls the GetShoppingCartItems method at the appropriate time in the page life cycle and automatically binds the returned data.
Then, the code uses that instance to return the items in the cart by calling the GetCartItems method. When either the ProductList. When they click the link, the application navigates to the processing page named AddToCart. Now, you'll add an Add to Cart link to both the ProductList. This link will include the product ID that is retrieved from the database.
Add the markup highlighted in yellow to the ProductList. Press F5 to run the application. After the project recreates the database, the browser will open and show the Default.
Select Cars from the category navigation menu. The ProductList. Click the Add to Cart link next to the first product listed the convertible car. Click the Add to Cart link next to the first product listed. In addition to adding products to the shopping cart, you will add a GetTotal method to the ShoppingCart class and display the total order amount in the shopping cart page.
Add the following GetTotal method highlighted in yellow to the ShoppingCart class, so that the class appears as follows:. First, the GetTotal method gets the ID of the shopping cart for the user. Then the method gets the cart total by multiplying the product price by the product quantity for each product listed in the cart. The above code uses the nullable type " int? Nullable types can represent all the values of an underlying type, and also as a null value.
For more information see, Using Nullable Types. Next you'll modify the code for the ShoppingCart. In the ShoppingCart.
When the ShoppingCart. If the shopping cart is empty, a message to that effect is displayed. Run the application now to see how you can not only add a product to the shopping cart, but you can see the shopping cart total. The browser will open and show the Default.
Click the Add To Cart link next to the first product. To allow the users to modify the shopping cart, you'll add an Update button and a Checkout button to the shopping cart page.
The Checkout button is not used until later in this tutorial series. In Solution Explorer , open the ShoppingCart. To add the Update button and the Checkout button to the ShoppingCart. This event handler will call the code that you'll add in the next step. Next, you can update the code contained in the ShoppingCart. When the user clicks the Update button on the ShoppingCart. The UpdateCartItems method gets the updated values for each item in the shopping cart.
A complete array of development tools and features in native macOS experience for every stage of your software development. Download Visual Studio for Mac. A complete array of development tools and features in one place to elevate and enhance every stage of your software development.
How to install offline Compare editions. Visual Studio built-in features empower full development cycle. This device is not currently supported for these products. To continue downloading, click here. Visual Studio Release notes. Compare Editions.
How to install offline. Powerful IDE, free for students, open-source contributors, and individuals Free download. Professional IDE best suited to small teams Free trial. Scalable, end-to-end solution for teams of any size Free trial. Get early access to latest features not yet in the main release Learn more. Visual Studio for Mac. Free download. Visual Studio Code. Free download Windows x64 User Installer. Linux x Learn more. Still not sure which tool is best for you?
We can help. Visual Studio Code for Windows. Download Visual Studio Code. Highlights Free code editor Built on open source and runs everywhere Hundreds of programming languages supported.
0コメント