public ActionResult Create(Models.Producers createPdc, HttpPostedFileBase file) { if (Session["accname"] == null) { Session["accname"] = null; return(RedirectToAction("Login", "Account")); } else { if (file != null) { if (file.ContentLength > 0) { try { string nameFile = Path.GetFileName(file.FileName); file.SaveAs(Path.Combine(Server.MapPath("/Image"), nameFile)); createPdc.pdcPhoto = "/Image/" + nameFile; } catch (Exception) { ViewBag.CreatePdcError = "Không thể chọn ảnh."; } } var pdc = dbPdc.Producers.SingleOrDefault(c => c.pdcName.Equals(createPdc.pdcName)); try { if (ModelState.IsValid) { if (pdc != null) { ViewBag.CreatePdcError = "Hãng sản xuất đã tồn tại."; } else { dbPdc.Producers.Add(createPdc); dbPdc.SaveChanges(); ViewBag.CreatePdcError = "Thêm hãng sản xuất thành công."; } } } catch (Exception) { ViewBag.CreatePdcError = "Không thể thêm hãng sản xuất."; } } else { ViewBag.HinhAnh = "Vui lòng chọn hình ảnh."; } return(View()); } }
public ActionResult Edit(Models.Producers editPdc, HttpPostedFileBase file) { if (Session["accname"] == null) { Session["accname"] = null; return(RedirectToAction("Login", "Account")); } else { if (file != null) { if (file.ContentLength > 0) { try { string nameFile = Path.GetFileName(file.FileName); file.SaveAs(Path.Combine(Server.MapPath("/Image"), nameFile)); editPdc.pdcPhoto = "/Image/" + nameFile; } catch (Exception) { ViewBag.EditPdcError = "Không thể chọn ảnh."; } } } try { if (ModelState.IsValid) { dbPdc.Entry(editPdc).State = EntityState.Modified; dbPdc.SaveChanges(); ViewBag.EditPdcError = "Cập nhật hãng sản xuất thành công."; } } catch (Exception) { ViewBag.EditPdcError = "Không thể cập nhật hãng sản xuất."; } return(View()); } }