public ActionResult Information() { if (Session["Email"] != null) { var id = Session["IdAdmin"]; var doctor = db.Doctors.Find(Convert.ToInt32(id)); var doc = new InforDoctorViewModelAdmin() { Id = Convert.ToInt32(id), Firstname = doctor.Firstname, Lastname = doctor.Lastname, Email = doctor.Email, Address = doctor.Address, Image = doctor.Image }; return(View(doc)); } else { return(RedirectToAction("Login", "Admin")); } }
public ActionResult Information(InforDoctorViewModelAdmin infor, HttpPostedFileBase photo) { var id = Session["IdAdmin"]; var doctor = db.Doctors.Find(Convert.ToInt32(id)); var docview = new InforDoctorViewModelAdmin() { Id = Convert.ToInt32(id), Firstname = doctor.Firstname, Lastname = doctor.Lastname, Email = doctor.Email, Address = doctor.Address, Image = doctor.Image }; var doc = db.Doctors.SingleOrDefault(a => a.Id.Equals(infor.Id)); if (photo != null) { if (!isValidContentType(photo.ContentType)) { ViewBag.Error = "Only JPG, JPEG, PNG & GIF files are allowed"; return(View(docview)); } else if (!isValidContentLenght(photo.ContentLength)) { ViewBag.Error = "Your file is too large"; return(View(docview)); } else { if (photo.ContentLength > 0) { // detele image old if (infor.Image != null) { var filePathDetele = Server.MapPath("~/Content/cssAdmin/app/media/img/users/" + infor.Image); if (System.IO.File.Exists(filePathDetele)) { System.IO.File.Delete(filePathDetele); } } // add image new var fileName = Path.GetFileName(photo.FileName); var path = Path.Combine(Server.MapPath("~/Content/cssAdmin/app/media/img/users/"), fileName); photo.SaveAs(path); doc.Image = photo.FileName; Session["Img"] = photo.FileName.ToString(); } } } doc.Id = infor.Id; doc.Firstname = infor.Firstname; doc.Lastname = infor.Lastname; doc.Email = infor.Email; doc.Address = infor.Address; db.SaveChanges(); return(RedirectToAction("Information", "Admin")); }