public IActionResult Edit(int id, Dog editedDog) { try { if (ModelState.IsValid) { _dogRepo.Edit(editedDog); return(RedirectToAction(nameof(Index))); } return(View(editedDog)); } catch { return(View(editedDog)); } }
public IActionResult Edit(Dog editedDog, IFormCollection collection) { try { // Note: when you have a post, you need to validate that the model validated. if (ModelState.IsValid) { _dogRepo.Edit(editedDog); return(RedirectToAction(nameof(Index))); } return(View(editedDog)); } catch { return(View(editedDog)); } }
public ActionResult Edit(DogViewModel editDog, IFormFile pic, IFormCollection collection) { try { if (!ModelState.IsValid) { return(View(editDog)); } // Remove deleted colors and set DogId foreach (Color clr in editDog.ThisDog.Colors.ToList()) { //if (clr.Name == "" || clr.Name == null) //{ // editDog.ThisDog.Colors.Remove(clr); //} if (clr.DogId == 0) { clr.DogId = editDog.ThisDog.Id; } } // add status to array editDog.ThisDog.Statuses = new List <Status>(); string lastStatus = _dogRepo.GetDogById(editDog.ThisDog.Id).CurrentStatus; if (editDog.currentStatus.DogStatus != lastStatus) { editDog.ThisDog.Statuses.Add(editDog.currentStatus); } editDog.ThisDog.CurrentStatus = editDog.currentStatus.DogStatus; if (pic != null) { Image newImage = new Image(); var filename = Path.Combine(_hostEnv.WebRootPath, "images", Path.GetFileName(pic.FileName)); using (var fileStream = new FileStream(filename, FileMode.Create)) { pic.CopyTo(fileStream); } // pic.CopyTo(new FileStream(filename, FileMode.Create)); newImage.Name = pic.FileName; newImage.DogId = editDog.ThisDog.Id; //Image noImage = new Image(); //noImage=editDog.ThisDog.Images.Find(p => p.Name == "noPhoto.jpg"); //if (noImage != null) //{ // editDog.ThisDog.Images.Remove(noImage); //} if (editDog.ThisDog.Images == null) { editDog.ThisDog.Images = new List <Image>(); } editDog.ThisDog.Images.Add(newImage); } _dogRepo.Edit(editDog.ThisDog); return(RedirectToAction(nameof(Index))); } catch (Exception ex) { _logger.LogError("Error on Update: " + ex.Message); return(View(editDog)); } }