コード例 #1
0
        public async Task <IActionResult> PlannedTripEdit(int id, EditPlannedTripViewModel viewModel)
        {
            ModelState.Remove("Trip.User");
            ModelState.Remove("Trip.UserId");

            //ApplicationUser user = await GetCurrentUserAsync();

            //viewModel.Trip.UserId = user.Id;

            if (ModelState.IsValid)
            {
                try
                {
                    Trip trip = await _context.Trip
                                .Include(t => t.TripTravelTypes)
                                .Include(t => t.TripVisitLocations)
                                .SingleOrDefaultAsync(t => t.TripId == id);

                    //This checks if there are any joiner tables of this kind for this trip,
                    //then it foreaches over the joiner table and delets each one from the db
                    //this deletes all TripTravelTypes the joiner tables
                    if (trip.TripTravelTypes.Count > 0)
                    {
                        foreach (TripTravelType travelType in trip.TripTravelTypes)
                        {
                            //this says for each one of the joiner tables put it in the _context bag to get deleted on _context.SaveChangesAsync
                            _context.Remove(travelType);
                        }
                    }

                    //this builds up TripTravelType tables for each TravelType thats selected
                    //checks to see if there are selectedTravelTypeIds to loop over
                    if (viewModel.SelectedTravelTypeIds != null)
                    {
                        //makes joiner table for TripTravelType
                        foreach (int TypeId in viewModel.SelectedTravelTypeIds)
                        {
                            TripTravelType newTripTT = new TripTravelType()
                            {   //pulls tripid out of context bag
                                TripId       = trip.TripId,
                                TravelTypeId = TypeId
                            };

                            _context.Add(newTripTT);
                        }
                    }

                    // This deletes all the TripVisitLocations joiner tables
                    if (trip.TripVisitLocations.Count > 0)
                    {
                        foreach (TripVisitLocation location in trip.TripVisitLocations)
                        {
                            _context.Remove(location);
                        }
                    }

                    //this builds up the TripVisitLocation for food and adds it to the db context
                    if (viewModel.NewFoodLocations.Count > 0)
                    {
                        foreach (TripVisitLocation location in viewModel.NewFoodLocations)
                        {
                            TripVisitLocation newTripVL = new TripVisitLocation()
                            {
                                TripId         = trip.TripId,
                                LocationTypeId = 1,
                                Name           = location.Name,
                                Description    = location.Description,
                                IsCompleted    = false
                            };
                            _context.Add(newTripVL);
                        }
                    }

                    //this builds up the TripVisitLocation for places and adds it to the db context
                    if (viewModel.NewVisitLocations.Count > 0)
                    {
                        foreach (TripVisitLocation location in viewModel.NewVisitLocations)
                        {
                            TripVisitLocation newTripVL = new TripVisitLocation()
                            {
                                TripId         = trip.TripId,
                                LocationTypeId = 2,
                                Name           = location.Name,
                                Description    = location.Description,
                                IsCompleted    = false
                            };
                            _context.Add(newTripVL);
                        }
                    }

                    trip.Location      = viewModel.Trip.Location;
                    trip.Accommodation = viewModel.Trip.Accommodation;
                    trip.Budget        = viewModel.Trip.Budget;
                    trip.ContinentId   = viewModel.Trip.ContinentId;
                    trip.IsPreTrip     = true;
                    trip.Title         = viewModel.Trip.Title;
                    trip.TripDates     = viewModel.Trip.TripDates;

                    _context.Update(trip);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction("PlannedTripDetails", "Trips", new { id = trip.TripId }));
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!TripExists(id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
            }

            return(View(viewModel));
        }
コード例 #2
0
        //--------------------------------------------------------------------------END FINISH TRIP CREATE

        //-----------------------------------------------------------------------START PLANNED TRIP EDIT
        // GET: Trips/Edit/5
        public async Task <IActionResult> PlannedTripEdit(int id)
        {
            Trip trip = await _context.Trip
                        .Include(t => t.TripTravelTypes)
                        .Include(t => t.TripVisitLocations)
                        .FirstOrDefaultAsync(t => t.TripId == id);

            if (trip == null)
            {
                return(NotFound());
            }

            List <Continent> AllContinents = await _context.Continent.ToListAsync();

            List <SelectListItem> allContinentOptions = new List <SelectListItem>();

            foreach (Continent c in AllContinents)
            {
                SelectListItem sli = new SelectListItem();
                sli.Text  = c.Name;
                sli.Value = c.ContinentId.ToString();
                allContinentOptions.Add(sli);
            }
            ;

            EditPlannedTripViewModel viewmodel = new EditPlannedTripViewModel
            {
                AllContinentOptions = allContinentOptions,
                Trip = trip,
                CurrentFoodLocations  = trip.TripVisitLocations.Where(VisitLoc => VisitLoc.LocationTypeId == 1).ToList(),
                CurrentVisitLocations = trip.TripVisitLocations.Where(VisitLoc => VisitLoc.LocationTypeId == 2).ToList()
            };

            //get TravelTypes
            List <TravelType> AllTravelTypes = _context.TravelType.ToList();

            //get a list of the travelTypes for this trip
            List <TravelType> PrevSelectedTravelTypes = _context.TripTravelType
                                                        .Include(t => t.TravelType)
                                                        .Where(t => t.TripId == trip.TripId)
                                                        .Select(t => t.TravelType)
                                                        .ToList();

            //makes an empty list to hold selectListItems
            List <SelectListItem> DisplayTripTravelTypes = new List <SelectListItem>();

            //this loops over allTravelTypes
            //any returns a bool of true or false base on if the condition that was passed in is met
            //I use the bool value it returns to set the checked value on the selectListItems for my check boxes
            foreach (TravelType TravelType in AllTravelTypes)
            {
                bool newList = PrevSelectedTravelTypes.Any(item => item.TravelTypeId == TravelType.TravelTypeId);
                DisplayTripTravelTypes.Add(new SelectListItem
                {
                    Text     = TravelType.Type,
                    Value    = TravelType.TravelTypeId.ToString(),
                    Selected = newList
                });
            }

            viewmodel.AllTravelTypes = DisplayTripTravelTypes;

            ViewData["scripts"] = new List <string>()
            {
                "EditPlannedTrip"
            };

            return(View(viewmodel));
        }