public ActionResult ThemMoi(Sach sach, HttpPostedFileBase fileUpload) { //Save the name of file upload ViewBag.MaChuDe = new SelectList(db.ChuDes.OrderBy(n => n.TenChuDe).ToList(), "MaChuDe", "TenChuDe"); ViewBag.MaNXB = new SelectList(db.NhaXuatBans.OrderBy(n => n.TenNXB).ToList(), "MaNXB", "TenNXB"); if (fileUpload == null) { ViewBag.ThongBao = "Chọn hình ảnh"; return View(); } if(ModelState.IsValid) { var fileName = Path.GetFileName(fileUpload.FileName); //Save the path of the file upload var filePath = Path.Combine(Server.MapPath("~/HinhAnhSP"), fileName); //Is the file upload exist ? if (System.IO.File.Exists(filePath)) { ViewBag.ThongBao = "Hinh anh da ton tai"; } else { fileUpload.SaveAs(filePath); } sach.AnhBia = fileName; db.Saches.Add(sach); db.SaveChanges(); } return View(); }
public ActionResult ChinhSua(Sach sach) { if(ModelState.IsValid) { db.Entry(sach).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); } ViewBag.MaChuDe = new SelectList(db.ChuDes.ToList(), "MaChuDe", "TenChuDe", sach.MaChuDe); ViewBag.MaNXB = new SelectList(db.NhaXuatBans.ToList(), "MaNXB", "TenNXB", sach.MaNXB); return View(sach); }