public ActionResult Edit(Guid?id, BookModel model) { if (model == null || ModelState.IsValid == false) { if (model == null && id != null) { model = AutoMapper.Mapper.Map <BookModel>(_booksRepository.Get(id.Value)); } return(View(model)); } else { var entity = id != null?_booksRepository.Get(id.Value) : new BookEntity(); AutoMapper.Mapper.Map(model, entity); if (model.Image.Upload != null && model.Image.Upload.ContentLength > 0) { var uploadDir = "~/uploads"; var filename = Guid.NewGuid().ToString() + ".jpg"; var mapPath = Server.MapPath(uploadDir); Directory.CreateDirectory(mapPath); var imagePath = Path.Combine(mapPath, filename); var imageUrl = Path.Combine(uploadDir, filename); model.Image.Upload.SaveAs(imagePath); entity.Image = imageUrl; } _booksRepository.AddOrUpdate(entity); return(View("_ClosePopup")); } }