Exemplo n.º 1
0
        public ActionResult ThemGioHang(string masp)
        {
            QuanLySanPhamBus   db = new QuanLySanPhamBus();
            SanPham            sp = db.LaySanPham(masp);
            List <itemGioHang> gh = null;

            if (Session["giohang"] == null)
            {
                itemGioHang a = new itemGioHang();
                a.MaSP    = sp.MaSP;
                a.TenSP   = sp.TenSP;
                a.HinhAnh = sp.HinhAnh;
                a.DonGia  = sp.DonGia - ((sp.DonGia * sp.GiamGia) / 100);
                a.SoLuong = 1;
                gh        = new List <itemGioHang>();
                gh.Add(a);
                Session["giohang"] = new List <itemGioHang>();
            }
            else
            {
                gh = Session["giohang"] as List <itemGioHang>;
                itemGioHang test = gh.FirstOrDefault(m => m.MaSP == masp);
                if (test == null)
                {
                    itemGioHang a = new itemGioHang();
                    a.MaSP    = sp.MaSP;
                    a.TenSP   = sp.TenSP;
                    a.HinhAnh = sp.HinhAnh;
                    a.DonGia  = sp.DonGia - ((sp.DonGia * sp.GiamGia) / 100);
                    a.SoLuong = 1;
                    gh.Add(a);
                }
                else
                {
                    test.SoLuong = test.SoLuong + 1;
                }
                Session["giohang"] = gh;
            }
            Session["giohang"] = gh;
            int tongtien = 0;
            int soluong  = 0;

            foreach (itemGioHang ct in gh)
            {
                tongtien += ct.SoLuong * ct.DonGia;
            }
            soluong = gh.Count;
            return(Json(new { success = true, ms = "Them san pham thanh cong", data = gh, tongtien = tongtien, Soluong = soluong }, JsonRequestBehavior.AllowGet));
        }