public void Update(Sach obj) { db.Entry(obj).State = System.Data.EntityState.Modified; }
public void Insert(Sach obj) { db.Saches.Add(obj); }
public void DeleteSach(Sach sach) { var m = db.Saches.Find(sach.Id); db.Saches.Remove(m); }
public ActionResult XoaSach(Sach sach) { repository.DeleteSach(sach); repository.Save(); return RedirectToAction("MainAdmin", "Admin"); }
public ActionResult ThemSach(Sach sach, HttpPostedFileBase imgUpload, HttpPostedFileBase fileUpload) { //var m = System.Web.HttpContext.Current.User.Identity; var m = User.Identity; BaiGiangContext db = new BaiGiangContext(); var userlogin = db.Users.FirstOrDefault(x => x.UserName == System.Web.HttpContext.Current.User.Identity.Name); ViewBag.HienThiLop = new SelectList(repository.ListLop(), "Id", "TenLop", "Id"); ViewBag.HienThiMon = new SelectList(repository.ListMon(), "Id", "TenMon", "Id"); ViewBag.HienThiLoai = new SelectList(repository.ListLoai(), "Id", "TenLoai", "Id"); if (imgUpload == null) { ViewBag.Mess = "Vui lòng chọn hình ảnh cho sách"; return View(); } if (fileUpload == null) { ViewBag.Mess = "Vui lòng chọn file cho sách"; return View(); } if (ModelState.IsValid) { //luu ten file var imgName = Path.GetFileName(imgUpload.FileName); var fileName = Path.GetFileName(fileUpload.FileName); //luu duong dan var pathImg = Path.Combine(Server.MapPath("/BookContent/BookImg"), imgName); var pathFile = Path.Combine(Server.MapPath("/BookContent/BookFile"), fileName); fileUpload.SaveAs(pathFile); //Kiem tra hinh anh da ton tai chua if (System.IO.File.Exists(pathImg)) { ViewBag.Mess = "Hình ảnh đã tồn tại"; } else { imgUpload.SaveAs(pathImg); } } //ViewBag.HienThiLop = sach.IdLop; sach.IdUser = userlogin.Id; sach.Img = imgUpload.FileName; sach.BookFile = fileUpload.FileName; sach.NgayDang = DateTime.Now; repository.Insert(sach); repository.Save(); ViewBag.Mess = "Đã thêm sách thành công"; return View(); }
public ActionResult SuaSach(Sach sach, HttpPostedFileBase imgUpload, HttpPostedFileBase fileUpload) { ViewBag.HienThiMon = new SelectList(repository.ListMon(), "Id", "TenMon", "Id"); ViewBag.HienThiLop = new SelectList(repository.ListLop(), "Id", "TenLop", "Id"); ViewBag.HienThiLoai = new SelectList(repository.ListLoai(), "Id", "TenLoai", "Id"); if (imgUpload == null) { ViewBag.Mess = "Vui lòng chọn hình ảnh cho sách"; return SuaSach(sach.Id); } if (fileUpload == null) { ViewBag.Mess = "Vui lòng chọn file cho sách"; return SuaSach(sach.Id); } if (ModelState.IsValid) { //luu ten file var imgName = Path.GetFileName(imgUpload.FileName); var fileName = Path.GetFileName(fileUpload.FileName); //luu duong dan var pathImg = Path.Combine(Server.MapPath("/BookContent/BookImg"), imgName); var pathFile = Path.Combine(Server.MapPath("/BookContent/BookFile"), fileName); fileUpload.SaveAs(pathFile); //Kiem tra hinh anh da ton tai chua if (System.IO.File.Exists(pathImg)) { ViewBag.Mess = "Hình ảnh đã tồn tại"; } else { imgUpload.SaveAs(pathImg); } } //ViewBag.HienThiLop = sach.IdLop; sach.Img = imgUpload.FileName; sach.BookFile = fileUpload.FileName; repository.Update(sach); repository.Save(); return RedirectToAction("MainAdmin", "Admin"); }