コード例 #1
0
        public PartialViewResult HeaderCart()
        {
            var cart = Session[cartSession];
            var list = new List <CartItem>();

            var listCartPetFood     = new List <CartPetFoodItem>();
            var listCartPet         = new List <CartPetItem>();
            var listCartPetToy      = new List <CartPetToyItem>();
            var listCartPetMedicine = new List <CartPetMedicineItem>();

            if (cart != null)
            {
                list = (List <CartItem>)cart;
                PetFoodModel pfm = new PetFoodModel();
                foreach (var item in list)
                {
                    //product is pet's food
                    if (item.productID.StartsWith("PFD"))
                    {
                        PetFood         pf   = pfm.getPetFoodByID(item.productID);
                        CartPetFoodItem cpfi = new CartPetFoodItem();
                        cpfi.Petfood  = pf;
                        cpfi.Quantity = item.Quantity;
                        listCartPetFood.Add(cpfi);
                    }
                    //product is pet
                    else if (item.productID.StartsWith("PET"))
                    {
                        Pet         p   = pfm.getPetByID(item.productID);
                        CartPetItem cpi = new CartPetItem();
                        cpi.pet      = p;
                        cpi.Quantity = item.Quantity;
                        listCartPet.Add(cpi);
                    }
                    //product os pet's medicine
                    else if (item.productID.StartsWith("PMD"))
                    {
                        PetMedicine         pm   = pfm.getPetMedicineByID(item.productID);
                        CartPetMedicineItem cpmi = new CartPetMedicineItem();
                        cpmi.petMedicine = pm;
                        cpmi.Quantity    = item.Quantity;
                        listCartPetMedicine.Add(cpmi);
                    }
                    //Product is Pet's toys
                    else
                    {
                        PetToys        pt   = pfm.getPetToyByID(item.productID);
                        CartPetToyItem cpti = new CartPetToyItem();
                        cpti.petToy   = pt;
                        cpti.Quantity = item.Quantity;
                        listCartPetToy.Add(cpti);
                    }
                }
                ViewBag.listCartP  = listCartPet;
                ViewBag.listCartPT = listCartPetToy;
                ViewBag.listCartPM = listCartPetMedicine;
                ViewBag.ListItem   = list;
            }
            return(PartialView(listCartPetFood));
        }
コード例 #2
0
        // GET: Cart
        public ActionResult Index()
        {
            //get cart
            var cart = Session[cartSession];
            var list = new List <CartItem>();
            //constructor lists
            var listCartPetFood     = new List <CartPetFoodItem>();
            var listCartPet         = new List <CartPetItem>();
            var listCartPetToy      = new List <CartPetToyItem>();
            var listCartPetMedicine = new List <CartPetMedicineItem>();

            //cart have item
            if (cart != null)
            {
                list = (List <CartItem>)cart;//get item in session cart
                PetFoodModel pfm = new PetFoodModel();
                foreach (var item in list)
                {
                    //product is pet's food
                    if (item.productID.StartsWith("PFD"))
                    {
                        PetFood         pf   = pfm.getPetFoodByID(item.productID);
                        CartPetFoodItem cpfi = new CartPetFoodItem();
                        cpfi.Petfood  = pf;
                        cpfi.Quantity = item.Quantity;
                        //add to list pet food
                        listCartPetFood.Add(cpfi);
                    }
                    //product is pet
                    if (item.productID.StartsWith("PET"))
                    {
                        Pet         p   = pfm.getPetByID(item.productID);
                        CartPetItem cpi = new CartPetItem();
                        cpi.pet      = p;
                        cpi.Quantity = item.Quantity;
                        //add to pet list
                        listCartPet.Add(cpi);
                    }
                    //product os pet's medicine
                    if (item.productID.StartsWith("PMD"))
                    {
                        PetMedicine         pm   = pfm.getPetMedicineByID(item.productID);
                        CartPetMedicineItem cpmi = new CartPetMedicineItem();
                        cpmi.petMedicine = pm;
                        cpmi.Quantity    = item.Quantity;
                        //add to pet medicine list
                        listCartPetMedicine.Add(cpmi);
                    }
                    //Product is Pet's toys
                    if (item.productID.StartsWith("PTS"))
                    {
                        PetToys        pt   = pfm.getPetToyByID(item.productID);
                        CartPetToyItem cpti = new CartPetToyItem();
                        cpti.petToy   = pt;
                        cpti.Quantity = item.Quantity;
                        //add to pet toy list
                        listCartPetToy.Add(cpti);
                    }
                }
                //set lists to ViewBag
                ViewBag.listCartP  = listCartPet;
                ViewBag.listCartPT = listCartPetToy;
                ViewBag.listCartPM = listCartPetMedicine;
            }
            return(View(listCartPetFood));
        }