Exemplo n.º 1
0
 public ActionResult Edit(DANHMUC dm)
 {
     if (ModelState.IsValid)
     {
         db.Entry(dm).State = System.Data.Entity.EntityState.Modified;
         db.SaveChanges();
     }
     return(RedirectToAction("Index"));
 }
        public ActionResult Edit(SANPHAM sp)
        {
            if (ModelState.IsValid)
            {
                // cập nhật thay đổi vào model
                db.Entry(sp).State = System.Data.Entity.EntityState.Modified;
                db.SaveChanges();
            }

            ViewBag.MADM  = new SelectList(db.DANHMUCs.ToList().OrderBy(n => n.TENDM), "MADM", "TENDM", sp.MADM);
            ViewBag.MANCC = new SelectList(db.NHACCs.ToList().OrderBy(n => n.TENNCC), "MANCC", "TENNCC", sp.MANCC);

            return(RedirectToAction("Index"));
        }
Exemplo n.º 3
0
        public ActionResult ThanhToan([Bind(Include = "MADH,TENKH,DIACHI,DIENTHOAI,EMAIL,TRANGTHAI,NGAYTAO,GHICHU")]
                                      DONHANG donhang, string tenkh, string diachi, string sdt, string email, string ghichu, string trangthai)
        {
            donhang.TENKH     = tenkh;
            donhang.DIACHI    = diachi;
            donhang.DIENTHOAI = sdt;
            donhang.EMAIL     = email;

            donhang.NGAYTAO = DateTime.Now;
            donhang.GHICHU  = ghichu;
            if (trangthai == "0")
            {
                donhang.TRANGTHAI = false;
            }
            else
            {
                donhang.TRANGTHAI = true;
            }

            var     cart = (Cart)Session[CartSession];
            decimal tong = 0;

            try
            {
                if (cart != null)
                {
                    db.DONHANGs.Add(donhang);
                    db.SaveChanges();
                    var ID = (from x in db.DONHANGs orderby x.MADH descending select x.MADH).Take(1).Single();
                    foreach (var item in cart.Lines)
                    {
                        var CTDonHang = new CTDONHANG();
                        CTDonHang.MASP = item.SANPHAM.MASP;
                        CTDonHang.MADH = ID;
                        CTDonHang.GIA  = item.SANPHAM.GIA - item.SANPHAM.GIAMGIA * item.SANPHAM.GIA / 100;
                        CTDonHang.SL   = item.Quantity;

                        db.CTDONHANGs.Add(CTDonHang);
                        tong += (item.SANPHAM.GIA - item.SANPHAM.GIAMGIA * item.SANPHAM.GIA / 100).GetValueOrDefault(0) * item.Quantity;

                        SANPHAM sp     = db.SANPHAMs.SingleOrDefault(n => n.MASP == CTDonHang.MASP);
                        int     sl1    = (int)sp.SOLUONG;
                        int     sl2    = item.Quantity;
                        int     conlai = sl1 - sl2;
                        sp.SOLUONG         = conlai;
                        db.Entry(sp).State = System.Data.Entity.EntityState.Modified;
                        db.SaveChanges();
                    }
                }
            }
            catch (DbEntityValidationException ex)
            {
                var errorMessages     = ex.EntityValidationErrors.SelectMany(x => x.ValidationErrors).Select(x => x.ErrorMessage);
                var fullErrorMessages = string.Join(";", errorMessages);
                var exceptionMessages = string.Concat(ex.Message, "The validation errors are: ", fullErrorMessages);
                throw new DbEntityValidationException(exceptionMessages, ex.EntityValidationErrors);
            }
            cart = null;
            Session[CartSession] = cart;
            return(RedirectToAction("ThanhCong"));
        }