public ActionResult Update(int id = 1) { var dao = new SlideDao(); var slide = dao.GetSlideById(id); if (slide == null) { return(RedirectToAction("Index")); } ViewBag.image = slide.Image; return(View(slide)); }
public ActionResult Update(Slide model, HttpPostedFileBase image, int id) { var dao = new SlideDao(); var c = dao.GetSlideById(id); ViewBag.image = c.Image; if (ModelState.IsValid) { // nếu model valid if (image != null) { // nếu có thay đổi hình ảnh string fileName = Path.GetFileName(image.FileName); string[] tokens = fileName.Split('.'); if (tokens[tokens.Count() - 1] == "png" || tokens[tokens.Count() - 1] == "jpg" || tokens[tokens.Count() - 1] == "jpeg" || tokens[tokens.Count() - 1] == "gif") { // nếu hình ảnh hợp lệ string folderPath = Path.Combine(Server.MapPath("/Assets/client/images"), fileName); image.SaveAs(folderPath); FuncUpdate(model, fileName, dao); return(RedirectToAction("Index")); } else { // nếu hình ảnh ko hợp lệ ModelState.AddModelError("", "File hình ảnh chưa phù hợp!"); return(View()); } } else { // nếu ko thay đổi hình ảnh string fileName = ""; FuncUpdate(model, fileName, dao); return(RedirectToAction("Index")); } } else { return(View()); } }