Exemple #1
0
        // GET: AddOneToTrainingServicesCart
        public async Task <IActionResult> AddOneToTrainingServicesCart(int?id)
        {
            // Code below copied from http://learningprogramming.net/net/asp-net-core-mvc/build-shopping-cart-with-session-in-asp-net-core-mvc/
            // and updated it.

            // Get the training services cart
            List <CartItemModel> cart = SessionHelper.GetObjectFromJson <List <CartItemModel> >(HttpContext.Session, "trainingServicesCart");

            // Check to see if the training service that was selected is in the training services cart
            int index = isExistTrainingService((int)id);

            // If the index is not equal -1
            if (index != -1)
            {
                // Update the training services cart cartQty
                cart[index].CartQty++;

                // Get the cartItemModel where the training services price id is equal to the selected training service id
                var cartItemModel = await _context.CartItemModel
                                    .FirstOrDefaultAsync(m => m.TrainingServicesPriceID == id);

                // Check to see if the cart item model is null
                if (cartItemModel == null)
                {
                    return(NotFound());
                }

                // Update the cart item model cart quantity
                cartItemModel.CartQty++;

                // If the modelState is valid, update the cart item in the database
                //detail of model state
                //https://stackoverflow.com/questions/881281/what-is-modelstate-isvalid-valid-for-in-asp-net-mvc-in-nerddinner#targetText=ModelState.IsValid%20tells%20you%20if,validation%20system%20you're%20using.

                if (ModelState.IsValid)
                {
                    try
                    {
                        _context.Update(cartItemModel);    // Will update the cart table
                        await _context.SaveChangesAsync(); // Save the changes
                    }
                    catch (DbUpdateConcurrencyException)
                    {
                        // If any errors, throw an exception
                        throw;
                    }
                }
            }

            SessionHelper.SetObjectAsJson(HttpContext.Session, "trainingServicesCart", cart);

            return(RedirectToAction("Index"));
        }
Exemple #2
0
        public async Task <IActionResult> Edit(int id, [Bind("pricePuppyID,PricePuppyDesc,pricePuppy,pricePuppyImageUrl")] PricingPuppyModel pricingPuppyModel)
        {
            if (id != pricingPuppyModel.pricePuppyID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(pricingPuppyModel);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!PricingModelExists(pricingPuppyModel.pricePuppyID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(pricingPuppyModel));
        }
        public async Task <IActionResult> Edit(int id, [Bind("trainingServicesPriceID,trainingName,priceTraining,trainingDesc")] TrainingServicesPriceModel trainingServicesPriceModel)
        {
            if (id != trainingServicesPriceModel.trainingServicesPriceID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(trainingServicesPriceModel);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!TrainingServicesPriceModelExists(trainingServicesPriceModel.trainingServicesPriceID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(trainingServicesPriceModel));
        }
        public async Task <IActionResult> Edit(int id, [Bind("TrainingId,DogName,TrainerId,GraduationDate,TrainingType")] TrainingDogModel trainingDogModel)
        {
            if (id != trainingDogModel.TrainingId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(trainingDogModel);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!TrainingDogModelExists(trainingDogModel.TrainingId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(trainingDogModel));
        }
Exemple #5
0
        // GET: TrainersModels
        public async Task <IActionResult> Index()
        {
            // Get the trainers
            var trainersModel = await _context.TrainersModel.ToListAsync();

            // Create an updatedTrainersModel array
            var updatedTrainersModel = new TrainersModel[trainersModel.Count];

            int i = 0;

            // For each trainer in trainersModel
            foreach (TrainersModel trainer in trainersModel)
            {
                // Get all the dogs that have been trained by trainer.TrainerId
                var trainingDogCount = await _context.TrainingDogModel.CountAsync(m => m.TrainerId == trainer.TrainerId);

                // Set the training dog count to the trainer dog number
                trainer.DogNumber = trainingDogCount;

                // Set the updated trainer to the updatedTrainersModel
                updatedTrainersModel[i] = trainer;

                if (ModelState.IsValid)
                {
                    try
                    {
                        _context.Update(trainer);
                        await _context.SaveChangesAsync();
                    }
                    catch (DbUpdateConcurrencyException)
                    {
                        throw;
                    }
                }

                i++;
            }

            return(View(updatedTrainersModel));
        }