public ActionResult Neighborhood(DomesticUnitNeighborhoodViewModel viewModel) { NeighborhoodDomesticUnits relation = new NeighborhoodDomesticUnits { DomesticUnitID = viewModel.DomesticUnit.DomesticUnitID, NeighborhoodID = viewModel.NeighborhoodID }; relation.DomesticUnit = repository.DomesticUnits .FirstOrDefault(d => d.DomesticUnitID == relation.DomesticUnitID); relation.Neighborhood = repository.Neighborhoods .FirstOrDefault(n => n.NeighborhoodID == relation.NeighborhoodID); if (ModelState.IsValid) { repository.SaveNeighborhoodDomesticUnit(relation); TempData["message"] = $"{relation.DomesticUnit.Name} is now located in {relation.Neighborhood.Name}"; return(RedirectToAction("Index")); } else { // if enters here there is something wrong with the data values return(View(relation)); } }
public ViewResult Neighborhood(Guid id) { DomesticUnitNeighborhoodViewModel viewModel = new DomesticUnitNeighborhoodViewModel { DomesticUnit = repository.DomesticUnits.FirstOrDefault(d => d.DomesticUnitID == id) }; NeighborhoodDomesticUnits relation = repository.NeighborhoodDomesticUnitsTable .FirstOrDefault(e => e.DomesticUnitID == viewModel.DomesticUnit.DomesticUnitID); viewModel.Neighborhoods = relation == null ? repository.Neighborhoods : repository.Neighborhoods.Where(n => n.NeighborhoodID != relation.NeighborhoodID); if (relation != null) { Neighborhood neighborhood = repository.Neighborhoods .FirstOrDefault(n => n.NeighborhoodID == relation.NeighborhoodID); viewModel.CurrentNeighborhoodName = neighborhood.Name; } return(View(viewModel)); }