public ActionResult Neighborhood(PlaceNeighborhoodViewModel viewModel) { NeighborhoodPlaces relation = new NeighborhoodPlaces { PlaceID = viewModel.Place.PlaceID, NeighborhoodID = viewModel.NeighborhoodID }; relation.Place = repository.Places .FirstOrDefault(p => p.PlaceID == relation.PlaceID); relation.Neighborhood = repository.Neighborhoods .FirstOrDefault(n => n.NeighborhoodID == relation.NeighborhoodID); if (ModelState.IsValid) { repository.SaveNeighborhoodPlace(relation); TempData["message"] = $"{relation.Place.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 NeighborhoodPlaces DeleteNeighborhoodPlace(Guid id) { NeighborhoodPlaces dbEntry = context.NeighborhoodPlaces .FirstOrDefault(n => n.PlaceID == id); if (dbEntry != null) { context.NeighborhoodPlaces.Remove(dbEntry); context.SaveChanges(); } return(dbEntry); }
public void SaveNeighborhoodPlace(NeighborhoodPlaces neighborhoodPlaces) { NeighborhoodPlaces dbEntry = context.NeighborhoodPlaces .FirstOrDefault(n => n.PlaceID == neighborhoodPlaces.PlaceID); if (dbEntry != null) { DeleteNeighborhoodPlace(neighborhoodPlaces.PlaceID); } context.NeighborhoodPlaces.Add(neighborhoodPlaces); context.SaveChanges(); }
public ViewResult Neighborhood(Guid id) { PlaceNeighborhoodViewModel viewModel = new PlaceNeighborhoodViewModel { Place = repository.Places.FirstOrDefault(p => p.PlaceID == id) }; NeighborhoodPlaces relation = repository.NeighborhoodPlacesTable .FirstOrDefault(e => e.PlaceID == viewModel.Place.PlaceID); 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)); }