public ActionResult Edit(Book editBook) { Book first = _bookContext.GetBook(editBook.Id); if (first == null) { return(HttpNotFound()); } _bookContext.Update(editBook); return(View(editBook)); }
public ActionResult UploadCover(FormCollection formCollection, HttpPostedFileBase imagefile) { int id = Convert.ToInt32(formCollection["BookID"]); if (id == -1) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Book book = _db.Query <Book>().FirstOrDefault(b => b.BookID == id); if (book == null) { return(HttpNotFound()); } if (imagefile.ContentLength > 0) { // width + height will force size, care for distortion //Exmaple: ImageUpload imageUpload = new ImageUpload { Width = 800, Height = 700 }; // height will increase the width proportionally //Example: ImageUpload imageUpload = new ImageUpload { Height= 600 }; // width will increase the height proportionally ImageUpload imageUpload = new ImageUpload { Width = 300 }; // rename, resize, and upload //return object that contains {bool Success,string ErrorMessage,string ImageName} ImageResult imageResult = imageUpload.RenameUploadFile(imagefile); if (imageResult.Success) { book.ImageUrl = imageResult.ImageName; _db.Update(book); _db.SaveChanges(); TempData["book"] = book; return(RedirectToAction("UploadCompleted")); } else { // use imageResult.ErrorMessage to show the error ViewBag.Error = imageResult.ErrorMessage; } } return(View()); }