Пример #1
0
        public ActionResult SuaQ(Quyen q)
        {
            bool result = false;

            db.Entry(q).State = System.Data.Entity.EntityState.Modified;
            if (db.SaveChanges() > 0)
            {
                result = true;
            }
            return(Json(result, JsonRequestBehavior.AllowGet));
        }
Пример #2
0
        public ActionResult SuaSanPham(SanPham sp, HttpPostedFileBase HinhAnh)
        {
            ViewBag.MaDM     = db.DanhMucSanPhams.OrderBy(c => c.TenDanhMuc);
            ViewBag.MaLoaiSP = db.LoaiSanPhams.OrderBy(c => c.TenLoai);
            if (HinhAnh != null)
            {
                if (HinhAnh.ContentLength > 0)
                {
                    if (HinhAnh.ContentType != "image/jpeg" && HinhAnh.ContentType != "image/png" && HinhAnh.ContentType != "image/jfif" && HinhAnh.ContentType != "image/jpg")
                    {
                        ViewBag.ThongBaoUpload = "Định dạng hình ảnh không hợp lệ , vui lòng kiểm tra lại";
                    }
                    else
                    {
                        // kiểm tra hình ảnh đã tồn tại chưa
                        var filename = Path.GetFileName(HinhAnh.FileName);
                        var path     = Path.Combine(Server.MapPath("~/Content/ImageSanPham"), filename);
                        if (System.IO.File.Exists(path))
                        {
                            ViewBag.ThonBaoUpload = "Hình ảnh  đã tồn tại, hãy chọn ảnh khác";
                        }
                        else
                        {
                            HinhAnh.SaveAs(path);
                        }
                        sp.HinhAnh = filename;
                    }
                }
            }

            if (ModelState.IsValid)
            {
                db.Entry(sp).State = System.Data.Entity.EntityState.Modified;
                if (db.SaveChanges() > 0)
                {
                    TempData["msg"] = "Update thành công";
                    return(RedirectToAction("Index"));
                }
            }
            else
            {
                ViewBag.ThongBao = "Dữ Liệu Update không hợp lệ";
                return(View(sp));
            }

            return(View(sp));
        }