public ActionResult Edit([Bind(Include = "MaSanPham,TenSanPham,Gia,Anh,PhanBiet,MaDanhMuc,HienTrang")] Spham spham, HttpPostedFileBase fileimg) { if (ModelState.IsValid) { if (fileimg != null) { var img = Path.GetFileName(fileimg.FileName); var pathimg = Path.Combine(Server.MapPath("~/Content/Layout/images"), img); if (fileimg == null) { ViewBag.Img = "Chose images"; return(View()); } else if (System.IO.File.Exists(pathimg)) { ViewBag.Img = "Images had exists"; } else { fileimg.SaveAs(pathimg); } spham.Anh = fileimg.FileName; } db.Entry(spham).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.MaDanhMuc = new SelectList(db.DanhMucs, "MaDanhMuc", "TenDanhMuc", spham.MaDanhMuc); return(View(spham)); }
public ActionResult DeleteConfirmed(int id) { foreach (CTHD item in db.CTHDs.Where(t => t.MaSP == id).ToList()) { db.CTHDs.Remove(item); db.SaveChanges(); } Spham spham = db.Sphams.Find(id); db.Sphams.Remove(spham); db.SaveChanges(); return(Redirect(Request.UrlReferrer.ToString())); }
// GET: Sphams/Edit/5 public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Spham spham = db.Sphams.Find(id); if (spham == null) { return(HttpNotFound()); } ViewBag.MaDanhMuc = new SelectList(db.DanhMucs, "MaDanhMuc", "TenDanhMuc", spham.MaDanhMuc); return(View(spham)); }