public ActionResult Edit(EditionModel edition, Guid editionId, string magazineName, string customMagazine, string topicName, HttpPostedFileBase editionCoverNew, string coverOld) { #region Validation // Validation of custom fields if (String.IsNullOrWhiteSpace(magazineName) && String.IsNullOrWhiteSpace(customMagazine)) { ModelState.AddModelError(nameof(magazineName), "The Magazine field is reqiured."); } if (!String.IsNullOrWhiteSpace(magazineName) && !String.IsNullOrWhiteSpace(customMagazine)) { ModelState.AddModelError(nameof(magazineName), "Select existing magazine or create new, not both."); } if (String.IsNullOrWhiteSpace(topicName)) { ModelState.AddModelError(nameof(topicName), "The Topic field is reqiured."); } if (String.IsNullOrWhiteSpace(customMagazine) && String.IsNullOrWhiteSpace(magazineName)) { ModelState.AddModelError(nameof(magazineName), "The Magazine field is reqiured."); } if (customMagazine.Length > 200 || magazineName.Length > 200) { ModelState.AddModelError(nameof(magazineName), "The Magazine name field must be less than 200 characters."); } if (edition.EditionMonth == 0) { ModelState.AddModelError(nameof(edition.EditionMonth), "The Edition month field is required."); } // Clear errors from model field, because we have our custom field ModelState[nameof(edition.EditionImage)].Errors.Clear(); if (editionCoverNew != null) { // if not null checks for type if (editionCoverNew.ContentType != "image/png" && editionCoverNew.ContentType != @"image/jpeg") { ModelState.AddModelError(nameof(edition.EditionImage), "Only png and jpeg allowed"); } // If not null and proper type checks if file is smaller than 3mb else if (editionCoverNew.ContentLength > 3000000) { ModelState.AddModelError(nameof(edition.EditionImage), "Cover must be smaller than 3mb."); } } #endregion // Get magazines for correct display for magazine select on create edition page var magazines = _magazineRepository.GetAll(); ViewBag.MagazineTitles = magazines.Select(a => a.MagazineName); // Get topics for correct display for topic select on create edition page var topics = _topicRepository.GetAll(); ViewBag.Topics = topics.Select(a => a.TopicName); if (ModelState.IsValid) { string magName = String.Empty; // Checks what custom field for magazine selection was selected if (!String.IsNullOrWhiteSpace(magazineName)) { magName = magazineName; } else { // If custom field was selected, the new Magazine is creating MagazineModel magazineCreated = new MagazineModel() { MagazineId = Guid.NewGuid(), MagazineName = customMagazine }; _magazineRepository.Create(magazineCreated); _magazineRepository.Save(); magName = customMagazine; } TopicModel topic = _topicRepository.GetByName(topicName); MagazineModel magazine = _magazineRepository.GetByName(magName); // Convert image to byte array byte[] bytes; if (editionCoverNew != null) { using (BinaryReader br = new BinaryReader(editionCoverNew.InputStream)) { bytes = br.ReadBytes(editionCoverNew.ContentLength); } } else { bytes = Convert.FromBase64String(coverOld); } edition.EditionImage = bytes; edition.EditionId = editionId; edition.MagazineId = magazine.MagazineId; edition.TopicId = topic.TopicId; _editionRepository.Update(edition); _editionRepository.Save(); return(RedirectToAction("Index", "Edition", new { magazine = magazine.MagazineName, year = edition.EditionYear, month = (int)edition.EditionMonth })); } return(View(edition)); }