示例#1
0
        public ActionResult AddItem(int productTypeId, int currentProductDetailId, int?ram, int?memory, string color, decimal price, int quantity, int productId)
        {
            FormatBus format = new FormatBus();

            string productType = db.ProductTypes.Where(m => m.ProductTypeId == productTypeId).Select(m => m.ProductTypeName).FirstOrDefault();
            string nameProduct = db.Products.Where(m => m.ProductId == productId).Select(m => m.ProductName).FirstOrDefault();

            ViewBag.ProductId   = productId;
            ViewBag.ProductType = productType;


            if (Session["shoppingcart"] == null)
            {
                List <ShoppingCartItemBrowser> listShoppingCart = new List <ShoppingCartItemBrowser>();
                if (productType == "Smartphone")
                {
                    string fileImage             = "/Assets/user/images/" + productType + "/" + productId + "-" + color + "-1.jpg";
                    ShoppingCartItemBrowser item = new ShoppingCartItemBrowser {
                        ProductDetailId = currentProductDetailId, ProductId = productId, Name = nameProduct, RamSize = ram, MemorySize = memory, Color = color, Price = price, FileImage = fileImage, Quantiny = quantity, ProductTypeName = productType
                    };
                    listShoppingCart.Add(item);
                }
                else if (productType == "Accessory")
                {
                    string fileImage             = "/Assets/user/images/" + productType + "/" + productId + "-" + color + "-1.jpg";
                    ShoppingCartItemBrowser item = new ShoppingCartItemBrowser {
                        ProductDetailId = currentProductDetailId, ProductId = productId, Name = nameProduct, Color = format.ColorFormat(color), Price = price, FileImage = fileImage, Quantiny = quantity, ProductTypeName = productType
                    };
                    listShoppingCart.Add(item);
                }
                Session["shoppingcart"] = listShoppingCart;
            }
            else
            {
                List <ShoppingCartItemBrowser> listShoppingCart = (List <ShoppingCartItemBrowser>)Session["shoppingcart"];
                if (productType == "Smartphone")
                {
                    if (listShoppingCart.Find(m => m.ProductDetailId == currentProductDetailId) == null)
                    {
                        string fileImage             = "/Assets/user/images/" + productType + "/" + productId + "-" + color + "-1.jpg";
                        ShoppingCartItemBrowser item = new ShoppingCartItemBrowser {
                            ProductDetailId = currentProductDetailId, ProductId = productId, Name = nameProduct, RamSize = ram, MemorySize = memory, Color = format.ColorFormat(color), Price = price, Quantiny = 1, FileImage = fileImage, ProductTypeName = productType
                        };
                        listShoppingCart.Add(item);
                    }
                    else
                    {
                        foreach (var item in listShoppingCart)
                        {
                            if (item.ProductDetailId == currentProductDetailId)
                            {
                                item.Quantiny += quantity;
                            }
                        }
                    }
                }
                else if (productType == "Accessory")
                {
                    if (listShoppingCart.Find(m => m.ProductDetailId == currentProductDetailId) == null)
                    {
                        string fileImage             = "/Assets/user/images/" + productType + "/" + productId + "-" + color + "-1.jpg";
                        ShoppingCartItemBrowser item = new ShoppingCartItemBrowser {
                            ProductDetailId = currentProductDetailId, ProductId = productId, Name = nameProduct, Color = color, Price = price, Quantiny = 1, FileImage = fileImage, ProductTypeName = productType
                        };
                        listShoppingCart.Add(item);
                    }
                    else
                    {
                        foreach (var item in listShoppingCart)
                        {
                            if (item.ProductDetailId == currentProductDetailId)
                            {
                                item.Quantiny += quantity;
                            }
                        }
                    }
                }
                Session["shoppingcart"] = listShoppingCart;
            }

            return(RedirectToAction("Index"));
        }
        // GET: ProductInformation
        public ActionResult Index(int productTypeId, int productId, int?currentProductDetailId, int?ram, int?memory, string color)
        {
            FormatBus format = new FormatBus();

            ViewBag.ProductTypeId = productTypeId;
            ViewBag.ProductId     = productId;

            ViewBag.ProductType = db.ProductTypes.Where(m => m.ProductTypeId == productTypeId).Select(m => m.ProductTypeName).FirstOrDefault();
            //Use this for view index
            ViewBag.ProductName = db.Products.Where(m => m.ProductId == productId).Select(m => m.ProductName).FirstOrDefault();

            //Find ProductId version by memory-ram-color
            if (currentProductDetailId == null)
            {
                if (ViewBag.ProductType == "Smartphone")
                {
                    ViewBag.CurrentProductDetailId = db.ProductPortableDevices.Where(m => m.ProductId == productId && m.RamSize == ram && m.MemorySize == memory && m.Color == color).Select(m => m.ProductPortableDeviceId).FirstOrDefault();
                }
                else if (ViewBag.ProductType == "Accessory")
                {
                    ViewBag.CurrentProductDetailId = db.ProductAccessories.Where(m => m.ProductId == productId && m.Color == color).Select(m => m.ProductAccessoryId).FirstOrDefault();
                }
            }
            else
            {
                ViewBag.CurrentProductDetailId = currentProductDetailId;
            }


            //Filter list All of version in product
            var productInformationList = new List <ProductInformation>();

            if (ViewBag.ProductType == "Smartphone")
            {
                var productSmartphoneList = db.ProductPortableDevices.Where(m => m.ProductId == productId).ToList();
                foreach (var item in productSmartphoneList)
                {
                    productInformationList.Add(new ProductInformation {
                        ProductDetailId = item.ProductPortableDeviceId, ProductId = item.ProductId, Color = item.Color, Price = item.Price, Quantiny = item.Quantiny, FileImage = item.FileImage, Display = item.Display, MemorySize = item.MemorySize, RamSize = item.RamSize
                    });

                    //This is find properties for Current Product
                    if (item.ProductPortableDeviceId == ViewBag.CurrentProductDetailId)
                    {
                        ViewBag.CurrentProductColor    = item.Color;
                        ViewBag.CurrentProductRam      = item.RamSize;
                        ViewBag.CurrentProductMemory   = item.MemorySize;
                        ViewBag.CurrentProductQuantity = item.Quantiny;
                        ViewBag.CurrentProductPrice    = item.Price;
                    }
                }
            }
            else if (ViewBag.ProductType == "Accessory")
            {
                var productAccessoryList = db.ProductAccessories.Where(m => m.ProductId == productId).ToList();
                foreach (var item in productAccessoryList)
                {
                    productInformationList.Add(new ProductInformation {
                        ProductDetailId = item.ProductAccessoryId, ProductId = item.ProductId, Color = item.Color, FileImage = item.ImageFile, Quantiny = item.Quantiny
                    });

                    //This is find properties for Current Product
                    if (item.ProductAccessoryId == ViewBag.CurrentProductDetailId)
                    {
                        ViewBag.CurrentProductColor    = item.Color;
                        ViewBag.CurrentProductQuantity = item.Quantiny;
                        ViewBag.CurrentProductPrice    = item.Price;
                    }
                }
            }
            ViewBag.ColorList = productInformationList.Select(m => m.Color).Distinct();

            if (ViewBag.ProductType == "Smartphone")
            {
                //Filter versiom distinc have model by ram-memory
                ViewBag.ProductVersionList = from c in productInformationList
                                             group c by new
                {
                    c.RamSize,
                    c.MemorySize
                } into grp
                select grp.First();
            }

            ViewBag.FileImage1 = "/Assets/user/images/" + ViewBag.ProductType + "/" + productId + "-" + ViewBag.CurrentProductColor + "-1" + ".jpg";
            ViewBag.FileImage2 = "/Assets/user/images/" + ViewBag.ProductType + "/" + productId + "-" + ViewBag.CurrentProductColor + "-2" + ".jpg";
            ViewBag.FileImage3 = "/Assets/user/images/" + ViewBag.ProductType + "/" + productId + "-" + ViewBag.CurrentProductColor + "-3" + ".jpg";

            ViewBag.QuantityPrdOrd = null;

            return(View(productInformationList));
        }