public ActionResult AddBook(BookViewModel bookViewModel) { if (bookViewModel == null) { return View("~/Views/Common/NotFound.cshtml", new NotFoundViewModel() {ElementName = "Book"}); } if (ModelState.IsValid) { var book = Mapper.Map<BookViewModel, Book>(bookViewModel); SaveCoverImageOnServer(bookViewModel, book); if (ModelState.Values.Any(x => x.Errors.Count >= 1)) { return View("Add", bookViewModel); } _bookService.Add(book); } else { ModelState.AddModelError(string.Empty, "The fields below must be filled."); return View("Add", bookViewModel); } return RedirectToAction("index"); }
private void SaveCoverImageOnServer(BookViewModel bookViewModel, Book book) { var fileName = bookViewModel.CoverImageBase.FileName; if (_fileFormatValidation.IsImageType(fileName)) { SaveOnServer(bookViewModel.CoverImageBase.FileName, Keys.CoverImageBase); book.CoverImageUrl = String.Concat(Keys.PathUploads + "/", bookViewModel.CoverImageBase.FileName); } else { ModelState.AddModelError(string.Empty, "Please upload a valid image file."); } }