public ActionResult ChooseDomesticUnit(SimDomesticUnitViewModel simDomesticUnit) { SimLives simLives = new SimLives { SimID = simDomesticUnit.Sim.SimID, DomesticUnitID = simDomesticUnit.DomesticUnitID }; simLives.Sim = repository.Sims .FirstOrDefault(s => s.SimID == simLives.SimID); simLives.DomesticUnit = repository.DomesticUnits .FirstOrDefault(d => d.DomesticUnitID == simDomesticUnit.DomesticUnitID); if (ModelState.IsValid) { repository.SaveSimLives(simLives); TempData["message"] = $"{simLives.Sim.Name} now lives in {simLives.DomesticUnit.Name}"; return(RedirectToAction("Index")); } else { //if enters here there is something wrong with the data values return(View(simDomesticUnit)); } }
public SimLives DeleteSimLives(Guid simID) { SimLives dbEntry = context.SimLives .FirstOrDefault(s => s.SimID == simID); if (dbEntry != null) { context.SimLives.Remove(dbEntry); context.SaveChanges(); } return(dbEntry); }
public void SaveSimLives(SimLives simLives) { SimLives dbEntry = context.SimLives .FirstOrDefault(s => s.SimID == simLives.SimID); if (dbEntry != null) { DeleteSimLives(simLives.SimID); } context.SimLives.Add(simLives); context.SaveChanges(); }
public ViewResult ChooseDomesticUnit(Guid id) { SimDomesticUnitViewModel simDomesticUnit = new SimDomesticUnitViewModel { Sim = repository.Sims.FirstOrDefault(s => s.SimID == id) }; SimLives simLives = repository.SimLivesTable .FirstOrDefault(s => s.SimID == simDomesticUnit.Sim.SimID); simDomesticUnit.DomesticUnits = simLives == null ? repository.DomesticUnits : repository.DomesticUnits.Where(d => d.DomesticUnitID != simLives.DomesticUnitID); if (simLives != null) { DomesticUnit domesticUnit = repository.DomesticUnits .FirstOrDefault(d => d.DomesticUnitID == simLives.DomesticUnitID); simDomesticUnit.CurrentDomesticUnitName = domesticUnit.Name; } return(View(simDomesticUnit)); }