public ActionResult Login(string email, string password)
 {
     using (var db = new Context())
     {
         var login = (from a in db.KhachHang
                      where a.email == email && a.matkhau == password
                      select a).ToList();
         if (login.Count() != 0)
         {
             var quyen = (from a in db.KhachHang
                          where a.email == email && a.matkhau == password
                          select a.quyen).FirstOrDefault();
             var id = (from a in db.KhachHang
                       where a.email == email && a.matkhau == password
                       select a.makh).FirstOrDefault();
             var ten = (from a in db.KhachHang
                        where a.email == email && a.matkhau == password
                        select a.ten).FirstOrDefault();
             Session.Add("id", id.ToString());
             Session.Add("taikhoan", email);
             Session.Add("matkhau", password);
             Session.Add("quyen", quyen.ToString());
             Session.Add("ten", ten.ToString());
             var appear = (from a in db.GioHang
                           where a.makh == id
                           select a).ToList();
             var appearproduct = ChiTietGioHangAction.ListCTGioHang();
             if (Session["Cart"] != null)
             {
                 if (appear.Count == 0)
                 {
                     GioHangAction.addGioHang(id, ten);
                     List <ChiTietGioHang> addCart = new List <ChiTietGioHang>();
                     addCart = (List <ChiTietGioHang>)Session["Cart"];
                     foreach (var a in addCart)
                     {
                         foreach (var b in appearproduct)
                         {
                             if (a.mactgiohang != b.mactgiohang)
                             {
                                 ChiTietGioHangAction.addCTGioHang(id, a.masanpham, a.soluong, a.thanhtien);
                             }
                             else
                             {
                                 ChiTietGioHangAction.DaTonTai(a.mactgiohang, a.soluong, a.thanhtien);
                             }
                         }
                     }
                     Session.Remove("Cart");
                 }
                 else
                 {
                     List <ChiTietGioHang> addCart = new List <ChiTietGioHang>();
                     addCart = (List <ChiTietGioHang>)Session["Cart"];
                     ChiTietGioHang findproduct = new ChiTietGioHang();
                     foreach (var a in addCart)
                     {
                         foreach (var b in appearproduct)
                         {
                             if (b.makh == id && b.masanpham == a.masanpham)
                             {
                                 findproduct = new ChiTietGioHang {
                                     mactgiohang = b.mactgiohang, makh = b.makh, masanpham = b.masanpham, soluong = b.soluong, thanhtien = b.thanhtien
                                 };
                             }
                         }
                         if (findproduct.masanpham == 0)
                         {
                             ChiTietGioHangAction.addCTGioHang(id, a.masanpham, a.soluong, a.thanhtien);
                         }
                         else if (findproduct != null)
                         {
                             ChiTietGioHangAction.DaTonTai(findproduct.mactgiohang, a.soluong, a.thanhtien);
                         }
                     }
                     Session.Remove("Cart");
                 }
             }
             else
             {
                 if (appear.Count == 0)
                 {
                     GioHangAction.addGioHang(id, ten);
                 }
             }
             if (quyen.ToString() == "User")
             {
                 return(RedirectToAction("Home", "TMDT"));
             }
             else
             {
                 return(RedirectToAction("DashBoard", "TMDT"));
             }
         }
         else
         {
             TempData["ErrorLogin"] = "******";
             return(RedirectToAction("Login", "TMDT"));
         }
     }
 }
 public ActionResult addCart(int productid, int quantity)
 {
     using (var db = new Context())
     {
         if (checkLogin() == false)
         {
             var    cart    = SanPhamAction.FindSanPham(productid);
             double summary = quantity * cart.gia;
             //var appear = (from a in listcartdetail
             //              where a.masanpham == productid && a.isdeleted == false
             //              select a).ToList();
             if (Session["Cart"] == null)
             {
                 List <ChiTietGioHang> listcartdetail = new List <ChiTietGioHang>();
                 ChiTietGioHang        cartdetail     = new ChiTietGioHang {
                     mactgiohang = listcartdetail.Count + 1, makh = 1, masanpham = productid, soluong = quantity, thanhtien = summary
                 };
                 listcartdetail.Add(cartdetail);
                 Session["Cart"] = listcartdetail;
             }
             else
             {
                 List <ChiTietGioHang> listcartdetail = new List <ChiTietGioHang>();
                 listcartdetail = (List <ChiTietGioHang>)Session["Cart"];
                 var appear = (from a in listcartdetail
                               where a.masanpham == productid && a.isdeleted == false
                               select a).ToList();
                 if (appear.Count == 0)
                 {
                     ChiTietGioHang cartdetail = new ChiTietGioHang {
                         mactgiohang = listcartdetail.Count + 1, makh = 1, masanpham = productid, soluong = quantity, thanhtien = summary
                     };
                     listcartdetail.Add(cartdetail);
                     Session["Cart"] = listcartdetail;
                 }
                 else
                 {
                     foreach (var productappear in listcartdetail)
                     {
                         if (productappear.masanpham == productid)
                         {
                             productappear.soluong   = productappear.soluong + quantity;
                             productappear.thanhtien = productappear.thanhtien + summary;
                         }
                     }
                     Session["Cart"] = listcartdetail;
                 }
             }
         }
         else
         {
             int    userid  = Convert.ToInt32(Session["id"]);
             var    appear  = db.ChiTietGioHang.Where(s => s.masanpham == productid && s.makh == userid).ToList();
             var    cart    = SanPhamAction.FindSanPham(productid);
             double summary = cart.gia * quantity;
             if (appear.Count == 0)
             {
                 ChiTietGioHangAction.addCTGioHang(userid, productid, quantity, summary);
             }
             else
             {
                 ChiTietGioHangAction.DaTonTai(appear[0].mactgiohang, quantity, summary);
             }
         }
         return(RedirectToAction("Feature", "TMDT"));
     }
 }