コード例 #1
0
        public async Task <IActionResult> AddNumberOfPeople(int?bookingId)
        {
            //if brochureId is null, redirect to browse brochures page
            if (bookingId == null)
            {
                return(RedirectToAction(nameof(BrochureController.Browse)));
            }

            //get the booking from the database
            var booking = await _context.Bookings
                          .Where(b => b.BookingId == bookingId)
                          .Include(b => b.Brochure)
                          .FirstOrDefaultAsync();

            //populate the model and inject into the page
            AddNumberOfPeopleViewModel model = new AddNumberOfPeopleViewModel
            {
                BookingId    = (int)bookingId,
                PeopleAdded  = 1,
                ErrorMessage = "",
                MaxPeople    = booking.Brochure.MaxPeople
            };

            return(View(model));
        }
コード例 #2
0
        public async Task <IActionResult> AddNumberOfPeople(AddNumberOfPeopleViewModel model)
        {
            //get booking from database
            var booking = await _context.Bookings.FindAsync(model.BookingId);

            //if booking is not empty
            if (booking != null)
            {
                //get brochure from database
                var brochure = await _context.Brochures.FindAsync(booking.BrochureId);

                if (model.PeopleAdded < 1)
                {
                    model.ErrorMessage = "Please enter a positive whole number";
                    return(View(model));
                }

                if (model.PeopleAdded > brochure.MaxPeople)
                {
                    model.ErrorMessage = "Too many people";
                    return(View(model));
                }


                //updating the total price of the booking
                booking.TotalPrice = brochure.PricePerPerson * model.PeopleAdded;
                _context.Bookings.Update(booking);
                await _context.SaveChangesAsync();

                //redirect to AddPeople action
                return(RedirectToAction(nameof(PersonController.AddPeople), new { bookingId = model.BookingId, numberOfPeople = model.PeopleAdded }));
            }

            //on fail, return the page
            return(RedirectToAction(nameof(AddNumberOfPeople), new { bookingId = model.BookingId }));
        }