コード例 #1
0
        public ActionResult ThemMoi([Bind(Include = "MaSanPham, TenSanPham, GiaSanPham, NgayNhap, MoTa,SoLuongTon, MaLoaiSanPham, MaHangSanXuat, Hinh")] SanPham sanpham, IEnumerable <HttpPostedFileBase> upload)
        {
            if (Session["DangNhapAdmin"] == null)
            {
                return(RedirectToAction("Index", "DangNhap"));
            }
            if (ModelState.IsValid)
            {
                if (upload != null)
                {
                    sanpham.HinhAnhs = new List <WineShop.Models.HinhAnh>();
                    foreach (var file in upload)
                    {
                        if (file != null && file.ContentLength > 0)
                        {
                            string extension = Path.GetFileNameWithoutExtension(file.FileName);
                            string timeUTC   = DateTime.Now.ToFileTimeUtc().ToString();
                            string a         = file.FileName.Replace(extension, timeUTC);

                            String logoPath = Path.GetFileName(a);
                            var    hinh     = new WineShop.Models.HinhAnh
                            {
                                TenHinh = logoPath,
                                BiXoa   = false
                            };
                            file.SaveAs(Path.Combine(Server.MapPath("~/Images/"), hinh.TenHinh));
                            sanpham.HinhAnhs.Add(hinh);
                        }
                    }
                }
                sanpham.SoLuongBan = 0;
                sanpham.SoLuocXem  = 0;

                sanpham.BiXoa = 0;
                db.SanPhams.Add(sanpham);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            List <LoaiSanPham> listLoaiSanPham = db.LoaiSanPhams.ToList <LoaiSanPham>();
            List <HangSanXuat> listHangSanXuat = db.HangSanXuats.ToList <HangSanXuat>();

            ViewBag.LoaiSanPhams = new SelectList(listLoaiSanPham, "MaLoaiSanPham", "TenLoaiSanPham");
            ViewBag.HangSanXuats = new SelectList(listHangSanXuat, "MaHangSanXuat", "TenHangSanXuat");

            return(View(sanpham));
        }
コード例 #2
0
        public ActionResult CapNhat([Bind(Include = "MaSanPham, TenSanPham, GiaSanPham, NgayNhap, SoLuongTon, MoTa, MaLoaiSanPham, MaHangSanXuat")] SanPham sanpham, IEnumerable <HttpPostedFileBase> upload, IEnumerable <String> GiaTri)
        {
            if (Session["DangNhapAdmin"] == null)
            {
                return(RedirectToAction("Index", "DangNhap"));
            }
            if (ModelState.IsValid)
            {
                SanPham sp = db.SanPhams.Single <SanPham>(s => s.MaSanPham == sanpham.MaSanPham);
                sp.TenSanPham    = sanpham.TenSanPham;
                sp.MoTa          = sanpham.MoTa;
                sp.NgayNhap      = sanpham.NgayNhap;
                sp.SoLuongTon    = sanpham.SoLuongTon;
                sp.GiaSanPham    = sanpham.GiaSanPham;
                sp.MaLoaiSanPham = sanpham.MaLoaiSanPham;
                sp.MaHangSanXuat = sanpham.MaHangSanXuat;
                if (GiaTri != null)
                {
                    var listHinhAnh = sp.HinhAnhs;
                    foreach (var r in GiaTri)
                    {
                        int     maHinh = Int16.Parse(r);
                        HinhAnh ha     = listHinhAnh.Single(h => h.Ma == maHinh);

                        string hinhCu = Request.MapPath("~/Images/" + ha.TenHinh);
                        if (System.IO.File.Exists(hinhCu))
                        {
                            System.IO.File.Delete(hinhCu);
                        }
                        //ha.BiXoa = true;
                        //ha.MaSanPham = null;
                    }
                    var hinhAndList = db.HinhAnhs.ToList();
                    foreach (var r in GiaTri)
                    {
                        int     maHinh = Int16.Parse(r);
                        HinhAnh ha     = hinhAndList.Single(h => h.Ma == maHinh);
                        db.HinhAnhs.Remove(ha);
                    }
                }

                var hinhanh = new List <HinhAnh>();

                foreach (var file in upload)
                {
                    if (file != null && file.ContentLength > 0)
                    {
                        string extension = Path.GetFileNameWithoutExtension(file.FileName);
                        string timeUTC   = DateTime.Now.ToFileTimeUtc().ToString();
                        string a         = file.FileName.Replace(extension, timeUTC);

                        String logoPath = Path.GetFileName(a);
                        var    hinh     = new WineShop.Models.HinhAnh
                        {
                            TenHinh = logoPath,
                            BiXoa   = false
                        };
                        file.SaveAs(Path.Combine(Server.MapPath("~/Images/"), hinh.TenHinh));
                        hinhanh.Add(hinh);
                    }
                }
                if (hinhanh.Count > 0)
                {
                    foreach (var r in hinhanh)
                    {
                        sp.HinhAnhs.Add(r);
                    }
                }

                int n = db.SaveChanges();

                return(RedirectToAction("Index"));
            }
            List <LoaiSanPham> listLoaiSanPham = db.LoaiSanPhams.ToList <LoaiSanPham>();
            List <HangSanXuat> listHangSanXuat = db.HangSanXuats.ToList <HangSanXuat>();

            ViewBag.LoaiSanPhams = new SelectList(listLoaiSanPham, "MaLoaiSanPham", "TenLoaiSanPham");
            ViewBag.HangSanXuats = new SelectList(listHangSanXuat, "MaHangSanXuat", "TenHangSanXuat");
            return(View(sanpham));
        }