Beispiel #1
0
        public void wish()
        {
            //wish
            var wish = SessionWish.GetObjectFromJsonWish <List <Item> >(HttpContext.Session, "wish");

            if (wish != null)
            {
                var total = wish.Sum(item => item.Quantity);
                ViewBag.totalwish = total;
                ViewBag.wish      = wish;
            }
            else
            {
                List <Item> wish1 = new List <Item>();
                SessionWish.SetObjectAsJsonWish(HttpContext.Session, "wish", wish1);
                ViewBag.wish      = wish1;
                ViewBag.totalwish = 0;
            }
            var count     = ViewBag.totalwish;
            var wishcombo = SessionWishCombo.GetObjectFromJsonWishCombo <List <ItemCombo> >(HttpContext.Session, "wishcombo");

            if (wishcombo != null)
            {
                var total = count + wishcombo.Sum(item => item.Quantity);
                ViewBag.totalwish = total;
                ViewBag.wishcombo = wishcombo;
            }
            else
            {
                List <ItemCombo> wish1 = new List <ItemCombo>();
                SessionWishCombo.SetObjectAsJsonWishCombo(HttpContext.Session, "wishcombo", wish1);
                ViewBag.wishcombo = wish1;
                ViewBag.totalwish = count;
            }
        }
        public IActionResult AddtoWishCombo(int?id)
        {
            var combos = from s in _context.Combo
                         select s;

            if (SessionWishCombo.GetObjectFromJsonWishCombo <List <ItemCombo> >(HttpContext.Session, "wishcombo") == null)
            {
                List <ItemCombo> wish = new List <ItemCombo>();
                wish.Add(new ItemCombo {
                    Combo = combos.First(s => s.Id == id), Quantity = 1
                });
                SessionWish.SetObjectAsJsonWish(HttpContext.Session, "wishcombo", wish);
            }
            else
            {
                List <ItemCombo> wish = SessionWishCombo.GetObjectFromJsonWishCombo <List <ItemCombo> >(HttpContext.Session, "wishcombo");
                int index             = isExistwish(id);
                if (index != -1)
                {
                    wish[index].Quantity = 1;
                }
                else
                {
                    wish.Add(new ItemCombo {
                        Combo = combos.First(s => s.Id == id), Quantity = 1
                    });
                }
                SessionWish.SetObjectAsJsonWish(HttpContext.Session, "wishcombo", wish);
            }
            return(Redirect(ControllerContext.HttpContext.Request.Headers["Referer"].ToString()));
        }
        public IActionResult Removewish(int?id)
        {
            List <ItemCombo> wish = SessionWishCombo.GetObjectFromJsonWishCombo <List <ItemCombo> >(HttpContext.Session, "wishcombo");
            int index             = isExistwish(id);

            wish.RemoveAt(index);
            SessionWish.SetObjectAsJsonWish(HttpContext.Session, "wishcombo", wish);
            return(Redirect(ControllerContext.HttpContext.Request.Headers["Referer"].ToString()));
        }
        private int isExistwish(int?id)
        {
            List <ItemCombo> wish = SessionWishCombo.GetObjectFromJsonWishCombo <List <ItemCombo> >(HttpContext.Session, "wishcombo");

            for (int i = 0; i < wish.Count; i++)
            {
                if (wish[i].Combo.Id.Equals(id))
                {
                    return(i);
                }
            }
            return(-1);
        }