public ActionResult SingleBuyJavaScript(int id)
        {
            List <ShoppingCartObjectModel> itemObjList = new List <ShoppingCartObjectModel>();


            if (TempData["itemList"] == null)
            {
                ShoppingCartObjectModel newItem = new ShoppingCartObjectModel();
                // here should be the logined customer's id
                newItem.CustomerID = Convert.ToInt32(Session["CustomerID"]);

                newItem.ProductID = id;
                newItem.Quantity  = Convert.ToInt32(Request.Form["buyQuantity"]);
                itemObjList.Add(newItem);
            }
            else
            {
                itemObjList = TempData["itemList"] as List <ShoppingCartObjectModel>;

                ShoppingCartObjectModel tempObj = itemObjList.Find(obj => id == obj.ProductID);

                if (tempObj != null)
                {
                    tempObj.Quantity += Convert.ToInt32(Request.Form["buyQuantity"]);
                }
                else
                {
                    ShoppingCartObjectModel addObj = new ShoppingCartObjectModel();
                    addObj.CustomerID = Convert.ToInt32(Session["CustomerID"]);
                    addObj.ProductID  = id;
                    addObj.Quantity   = Convert.ToInt32(Request.Form["buyQuantity"]);


                    itemObjList.Add(addObj);
                }
            }

            TempData["itemList"] = itemObjList;
            TempData.Keep("itemList");

            return(RedirectToAction("CountShoppingCartItemTotal", "OptionalItem"));
        }
        public ActionResult CartItemDelete(int?id, PartialListModel partialModelList)
        {
            List <ShoppingCartObjectModel> itemObjList = new List <ShoppingCartObjectModel>();

            itemObjList = TempData["itemList"] as List <ShoppingCartObjectModel>;
            TempData.Keep("itemList");

            // remove from itemList, or say itemObjList
            ShoppingCartObjectModel itemToRemove = itemObjList.SingleOrDefault(r => r.ProductID == id);

            if (itemToRemove != null)
            {
                itemObjList.Remove(itemToRemove);
            }

            PartialListItemModel itemToRemoveFromPartialModel = partialModelList.PartialList.SingleOrDefault(r => r.PartialListProductId == id);

            if (itemToRemoveFromPartialModel != null)
            {
                partialModelList.PartialList.Remove(itemToRemoveFromPartialModel);
            }

            // copy the quantity from the model posted from delete button,
            // to the itemList, then save the new quantity to tempData.
            foreach (ShoppingCartObjectModel item in itemObjList)
            {
                PartialListItemModel itemToCopy = partialModelList.PartialList.SingleOrDefault(r => r.PartialListProductId == item.ProductID);
                item.Quantity = itemToCopy.PartialListProductBuyQuantity;
            }



            TempData["itemList"] = itemObjList;
            TempData.Keep("itemList");
            return(RedirectToAction("ShoppingCart"));
        }
        public ActionResult Index(int?id)
        {
            if (Request.Form["okOrCancel"] == "ok")
            {
                // a string: 2,5,7,8
                // each number is ProductID
                string productIdstring = Request.Form["boxItem"];

                List <ShoppingCartObjectModel> itemObjList = new List <ShoppingCartObjectModel>();

                if (string.IsNullOrEmpty(productIdstring))
                {
                    productIdstring = "";
                    // to ShoppingCart to check again
                    // or to a view show you buy nothing.
                    TempData.Keep("itemList");
                    return(RedirectToAction("ShoppingCart"));
                }


                // add petbox
                productIdstring += ",2";

                string[] productIdStringArray = productIdstring.Split(',');

                // translate from an array of string to an array of int
                int[] productIdIntArray = productIdStringArray
                                          .Select(s => { int i; return(int.TryParse(s, out i) ? i : (int?)null); })
                                          .Where(i => i.HasValue)
                                          .Select(i => i.Value)
                                          .ToArray();


                // user choose some items, and no itemList exist yet

                if (TempData["itemList"] == null)
                {
                    foreach (var item in productIdIntArray)
                    {
                        ShoppingCartObjectModel instance = new ShoppingCartObjectModel();

                        // here instance.CustomerID should be equal to Session[CustomerID]
                        instance.CustomerID = Convert.ToInt32(Session["CustomerID"]);
                        instance.ProductID  = item;
                        instance.Quantity   = 1;

                        itemObjList.Add(instance);
                    }
                }
                // if there is item in itemList
                else
                {
                    itemObjList = TempData["itemList"] as List <ShoppingCartObjectModel>;

                    foreach (int itemId in productIdIntArray)
                    {
                        ShoppingCartObjectModel tempObj = itemObjList.Find(obj => itemId == obj.ProductID);

                        if (tempObj != null)
                        {
                            tempObj.Quantity++;
                        }
                        else
                        {
                            ShoppingCartObjectModel addObj = new ShoppingCartObjectModel();
                            addObj.CustomerID = Convert.ToInt32(Session["CustomerID"]);
                            addObj.ProductID  = itemId;
                            addObj.Quantity   = 1;
                            itemObjList.Add(addObj);
                        }
                    }
                }

                TempData["itemList"] = itemObjList;
                TempData.Keep("itemList");

                return(RedirectToAction("ShoppingCart"));
            }

            if (TempData["shoppingURL"] != null)
            {
                string   returnPath = TempData["shoppingURL"].ToString();
                string[] pathArray  = returnPath.Split('/');

                return(RedirectToAction(pathArray[1], pathArray[0]));
            }
            else
            {
                return(RedirectToAction("", "Customer"));
            }


            // back to index of shopping
            //return RedirectToAction("", "");
        }