public ActionResult Create(EditionViewModel model) { if (ModelState.IsValid) { Editions edition = new Editions(); edition.InjectFrom(model); var createNewEdition = editionService.AddEdition(edition); if (createNewEdition == null) { ModelState.AddModelError("Name", "The Name must be unique!"); return(View(model)); } } return(RedirectToAction(nameof(Index))); }
public ActionResult AddEdition(EditionEditViewModel model) { if (!ModelState.IsValid) { return(View("CreateEdition", model)); } var extensionCover = Path.GetExtension(model.Cover.FileName); if (!(extensionCover == ".png" || extensionCover == ".jpg")) { ModelState.AddModelError("", @"Extension for cover should be '.png' or '.jpg'"); return(View("CreateEdition", model)); } var extensionFile = Path.GetExtension(model.File.FileName); if (extensionFile != ".pdf") { ModelState.AddModelError("", @"Extension for file should be '.pdf'"); return(View("CreateEdition", model)); } model.FileName = Guid.NewGuid() + extensionFile; model.CoverName = Guid.NewGuid() + extensionCover; model.File.SaveAs(Server.MapPath("../Storage/" + model.FileName)); model.Cover.SaveAs(Server.MapPath("../Storage/" + model.CoverName)); var edition = new Edition(); Mapper.Map(model, edition); _editionService.AddEdition(edition); return(RedirectToAction("GetAllEditionsForPublisher")); }