Introduction
As we know, this is the era of Computer Science. Computer Science makes our life much easier. All of us are very familiar with online shopping. I think a person familiar with Computer Science usually uses online shopping because it is very beneficial for us. Online shopping sites are places where many items are available for us and we can purchase those items from there. So online shopping is time-saving and also money-saving compared to going to a market and purchasing a product. So in this article I will simply explain online shopping cart methods and how they work.
Online shopping carts
The simplest and one-line definition of a cart in simple language is a bucket. Yes, a cart is just like a bucket. We choose the items available on online shopping sites and drag and drop those items to our cart. Here a cart tells us how many items you intend to purchase now. I think everyone is very familiar with this concept. It works simply by drag and drop operations. Some websites provide the option to go to the item and there is a button for adding this item to your cart. When you click on this button your item will be added into your cart. So here we will see how to make a cart in MVC for online shopping.
Procedure for making a drag-and-drop-able shopping cart
Step 1
Open your Visual Studio, go to the file menu then go to the new project and the new project and add the new ASP.NET web application.
Step 2
Select the MVC application and click on the OK button.
Step 3
Now you have a Solution Explorer and in the Solution Explorer you have multiple folders like Controllers, Views and many more.
Step 4
Go to the controller folder and add a new controller, here I add an empty controller and named it as the shopping controller.
When we do this then there is an option to please add an empty Controller.
Step 5
When we add the controller to our web project the code of the controller will be similar to the following code.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
-
- namespace shoppingchart.Controllers
- {
- public class shoppingController : Controller
- {
-
-
-
- public ActionResult Index()
- {
- return View();
- }
- }
- }
Step 6
After adding the controller we go to its ActionResult Index method and right-click on it and click on add view.
Step 7
After adding the view to your project add this code to your view.
- @{
- ViewBag.Title = "hi this is shopping cart example";
- }
- <link rel="stylesheet" href="http://code.jquery.com/ui/1.11.1/themes/smoothness/jquery-ui.css">
- <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
- <script src="http://code.jquery.com/ui/1.11.1/jquery-ui.js"></script>
- <style>
- #droppable
- {
- width: 400px;
- height: 150px;
- padding: 2.0em;
- float: right;
- margin: 20px;
- }
- </style>
- <script>
- var totalItem = 0;
- $(function () {
- $(".ui-widget-content").draggable({
- appendTo: "body",
- helper: "clone"
- });
- $("#shoppingcart").droppable({
- drop: function (event, ui) {
- totalItem = parseInt(totalItem) + 1
- $("#totalitem").html("total item add by you in cart=: " + totalItem);
-
- }
- });
- });
- </script>
-
- <div id="items" style="width: 1000px">
- <div style="text-align: center; float: left; width: 150px; height: 120px;" class="ui-widget-content">
- <img src="../../Images/tshirt.png" />
- <br />
- <b><span>T shirt</span></b>
- </div>
- <div style="text-align: center; float: left; width: 150px; height: 120px;" class="ui-widget-content">
- <img src="../../Images/books.png" />
- <br />
- <b><span>Books</span></b>
- </div>
- <div style="text-align: center; float: left; width: 150px; height: 120px;" class="ui-widget-content">
- <img src="../../Images/coldrinks.png" />
- <br />
- <b><span>coldrinks</span></b>
- </div>
- <div style="text-align: center; float: left; width: 150px; height: 120px;" class="ui-widget-content">
- <img src="../../Images/crockery.png" />
- <br />
- <b><span>Crockery</span></b>
- </div>
- <div style="text-align: center; float: left; width: 150px; height: 120px;" class="ui-widget-content">
- <img src="../../Images/mobile.png" />
- <br />
- <b><span>Mobile</span></b>
- </div>
- <div style="text-align: center; float: left; width: 150px; height: 120px;" class="ui-widget-content">
- <img src="../../Images/sandels.png" />
- <br />
- <b><span>Sandels</span></b>
- </div>
-
- <div id="items" style="width: 1000px">
- <div style="text-align: center; float: left; width: 150px; height: 120px;" class="ui-widget-content">
- <img src="../../Images/sports.png" />
- <br />
- <b><span>Sports goods</span></b>
- </div>
- <div style="text-align: center; float: left; width: 150px; height: 120px;" class="ui-widget-content">
- <img src="../../Images/tv.png" />
- <br />
- <b><span>Telivisions</span></b>
- </div>
- </div>
-
- <div id="shoppingcart">
-
- <img src="../../Images/cart.png" /><br />
-
- <b><span id="totalitem" style="color:red"></span></b>
- </div>
Step 8
When you add the preceding code to the Index view, in the code you have seen also some images with the path. So please add a new folder to your application and name it Images and paste those images into that folder that you have used in your application just like in the following picture.
Step 8
Since here we can see in the view code that we used a script file. This script file is used to count the items that were dropped by you into the cart, here we also used the jQuery for the drag and drop items in our cart. So here we use the two jQuery and one stylesheet file.
- <link rel="stylesheet" href="http://code.jquery.com/ui/1.11.1/themes/smoothness/jquery-ui.css">
- <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
- <script src="http://code.jquery.com/ui/1.11.1/jquery-ui.js"></script>
Step 9
Finally when we run our project or web application we see the output like this. Here we can drag and drop two items to the cart so it shows the message that the total number of items added by you to the cart is two, so the output is totally dependant on the items that you dragged and dropped to the cart depending on your needs or requirements.
Output
Summary
In this article we learned how online shopping carts work. As usual whenever we do online shopping there is always a cart available and we choose items from the available items and add those items to our cart. Our cart tells us how many items were chosen by us from the items list. So finally I can say that when online shopping, a shopping cart plays a very vital role from the end users view because it provides the total number of items that have chosen. Some websites also show the price of the items chosen. I think this article will be helpful for you to understand how online shopping carts work and you can easily create a draggable online shopping cart in a MVC application.