public ActionResult Dangky(khachhang kh, string url)
 {
     if (ModelState.IsValid)
     {
         //cho dữ liệu vào databse ->table khachhang
         db.khachhangs.Add(kh);
         //lưu vào cơ sở dữ liệu
         db.SaveChanges();
         Session["Taikhoan"]    = kh;
         Session["tentaikhoan"] = kh.khachhang_taikhoan;
         return(RedirectToAction("Index", "Home"));
     }
     return(View());
 }
        public ActionResult Dathang()
        {
            //kiểm tra đăng nhập
            if (Session["Taikhoan"] == null || Session["Taikhoan"].ToString() == "")
            {
                return(RedirectToAction("Dangnhap", "Nguoidung"));
            }

            //Kiểm tra Hóa đơn
            if (Session["Giohang"] == null)
            {
                RedirectToAction("Index", "Home");
            }
            //thêm đơn đặt hàng
            hoadon         hd = new hoadon();
            khachhang      kh = (khachhang)Session["Taikhoan"];
            List <Giohang> gh = LayGiohang();

            hd.khachhang_id = kh.khachhang_id;
            hd.ngaydat      = DateTime.Now;

            db.hoadons.Add(hd);

            //lưu vào hóa đơn
            db.SaveChanges();

            //thêm chi tiết hóa đơn
            foreach (var item in gh)
            {
                chitiethoadon cthd = new chitiethoadon();
                cthd.hoadon_id  = hd.hoadon_id;
                cthd.sanpham_id = item.sanpham_id;
                cthd.soluong    = item.soluong_sp;
                cthd.dongia     = (Int32)item.gia_sp;
                db.chitiethoadons.Add(cthd);
            }
            //lưu vào chi tiết hóa đơn
            db.SaveChanges();
            return(RedirectToAction("Index", "Home"));
        }
        //[ValidateInput(false)]  //thuộc tính

        public ActionResult Themmoi(sanpham sp, HttpPostedFileBase UrlAnh)
        {
            ViewBag.theloai_id = new SelectList(db.theloaisps.ToList(), "theloai_id", "theloai_name");
            //kiểm tra đường dẫn ảnh bìa
            if (UrlAnh == null)
            {
                ViewBag.Thongbao = "Chọn hình ảnh";
                return(View());
            }

            //thêm vào cơ sở dữ liệu
            //kiểm tra sự tồn tại bên csdl

            if (ModelState.IsValid)
            {
                //lưu tên file
                var fileName = Path.GetFileName(UrlAnh.FileName);

                //lưu đường dẫn
                var path = Path.Combine(Server.MapPath("~/Hinhanh/hinhanhsp"), fileName);

                //kiểm tra hình ảnh đã tồn tại hay chưa
                if (System.IO.File.Exists(path))
                {
                    ViewBag.Thongbao = "Hình ảnh đã tồn tại";
                }
                else
                {
                    UrlAnh.SaveAs(path);
                }
                sp.hinhanh_sp = UrlAnh.FileName;
                db.sanphams.Add(sp);
                db.SaveChanges();
            }

            return(View());
        }
 public ActionResult Themmoi(khachhang kh)
 {
     db.khachhangs.Add(kh);
     db.SaveChanges();
     return(RedirectToAction("Index"));
 }