public ActionResult DomesticUnit(PetDomesticUnitViewModel viewModel) { PetLives relation = new PetLives { PetID = viewModel.Pet.PetID, DomesticUnitID = viewModel.DomesticUnitID }; relation.Pet = repository.Pets .FirstOrDefault(p => p.PetID == relation.PetID); relation.DomesticUnit = repository.DomesticUnits .FirstOrDefault(d => d.DomesticUnitID == relation.DomesticUnitID); if (ModelState.IsValid) { repository.SavePetLives(relation); TempData["message"] = $"{relation.Pet.Name} now lives in {relation.DomesticUnit.Name}"; return(RedirectToAction("Index")); } else { // if enters here there is something wrong with the data values return(View(relation)); } }
public PetLives DeletePetLives(Guid id) { PetLives dbEntry = context.PetLives .FirstOrDefault(n => n.PetID == id); if (dbEntry != null) { context.PetLives.Remove(dbEntry); context.SaveChanges(); } return(dbEntry); }
public void SavePetLives(PetLives petLives) { PetLives dbEntry = context.PetLives .FirstOrDefault(n => n.PetID == petLives.PetID); if (dbEntry != null) { DeletePetLives(petLives.PetID); } context.PetLives.Add(petLives); context.SaveChanges(); }
public ViewResult DomesticUnit(Guid id) { PetDomesticUnitViewModel viewModel = new PetDomesticUnitViewModel { Pet = repository.Pets.FirstOrDefault(p => p.PetID == id) }; PetLives relation = repository.PetLivesTable .FirstOrDefault(p => p.PetID == id); viewModel.DomesticUnits = relation == null ? repository.DomesticUnits : repository.DomesticUnits.Where(d => d.DomesticUnitID != relation.DomesticUnitID); if (relation != null) { DomesticUnit domesticUnit = repository.DomesticUnits .FirstOrDefault(d => d.DomesticUnitID == relation.DomesticUnitID); viewModel.CurrentDomesticUnitName = domesticUnit.Name; } return(View(viewModel)); }