public ActionResult Edit(CreateComputerTypeViewModel model) { if (ModelState.IsValid) { // uploaded file will overwrite the old one. if (model.UploadedFile != null && model.UploadedFile.ContentLength > 0) { string directory = Server.MapPath("~/Content/TypeImages/"); string filename = model.ComputerType.Id + Path.GetExtension(model.UploadedFile.FileName); var path = Path.Combine(directory, filename); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } model.UploadedFile.SaveAs(path); model.ComputerType.ImagePath = "/Content/TypeImages/" + filename; model.ComputerType.Filename = filename; } // Database db.ComputerTypes.AddOrUpdate(model.ComputerType); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(model)); }
public ActionResult Create(CreateComputerTypeViewModel model) { if (ModelState.IsValid) { // Save to DB to get the ID db.ComputerTypes.Add(model.ComputerType); db.SaveChanges(); // uploaded file if (model.UploadedFile != null && model.UploadedFile.ContentLength > 0) { string directory = Server.MapPath("~/Content/TypeImages/"); string filename = model.ComputerType.Id + Path.GetExtension(model.UploadedFile.FileName); var path = Path.Combine(directory, filename); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } model.UploadedFile.SaveAs(path); model.ComputerType.ImagePath = "/Content/TypeImages/" + filename; model.ComputerType.Filename = filename; // Save again to save the image properties. db.ComputerTypes.AddOrUpdate(model.ComputerType); db.SaveChanges(); } return(RedirectToAction("Index")); } return(View(model)); }
public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } CreateComputerTypeViewModel model = new CreateComputerTypeViewModel(); model.ComputerType = db.ComputerTypes.Find(id); if (model.ComputerType == null) { return(HttpNotFound()); } return(View(model)); }