public ActionResult Edit([Bind(Include = "Id,Name,Image,Degree,Fees,SpecialistId")] Doctor doctor, HttpPostedFileBase Image, string pastImage) { doctor.Image = pastImage; if (Image != null && Image.ContentLength > 0) { string fullPath = Request.MapPath("~/" + pastImage); if (System.IO.File.Exists(fullPath)) { System.IO.File.Delete(fullPath); } try { string fileName = Guid.NewGuid().ToString() + System.IO.Path.GetExtension(Image.FileName); string uploadUrl = Server.MapPath("~/Picture"); Image.SaveAs(Path.Combine(uploadUrl, fileName)); doctor.Image = "Picture/" + fileName; } catch (Exception ex) { ViewBag.Error = "ERROR:" + ex.Message.ToString(); } } if (ModelState.IsValid) { db.Entry(doctor).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("DocIndex")); } return(View(doctor)); }
public ActionResult Edit([Bind(Include = "Id,HouseNo,Image,Information,Price")] House house, HttpPostedFileBase Image, string pastImage) { if (ModelState.IsValid) { if (Image != null && Image.ContentLength > 0) { try { string fileName = Guid.NewGuid().ToString() + System.IO.Path.GetExtension(Image.FileName); string uploadUrl = Server.MapPath("~/Picture"); Image.SaveAs(Path.Combine(uploadUrl, fileName)); house.Image = "Picture/" + fileName; } catch (Exception ex) { ViewBag.Message = "ERROR:" + ex.Message.ToString(); } } else { house.Image = pastImage; } db.Entry(house).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("HouseIndex")); } return(View(house)); }