public async Task <IActionResult> Pay() { var idd = HttpContext.Session.Get <Dictionary <int, int> >("ls")[int.MaxValue]; if (CreditCard.AppointmentId != idd) { return(NotFound()); } if (!ModelState.IsValid) { return(View(CreditCard)); } var id = CreditCard.AppointmentId; CreditCard.Appointments = null; List <ProductsSelectedForAppointment> psd = null; if (orm == 1) { qdb.incredit(CreditCard); psd = qdb.retpsa_with_ai(id); foreach (var item in psd) { var product = qdb.retProduct(item.ProductId); product.Count -= item.Count; if (product.Count == 0) { product.Available = false; } qdb.upProduct(product); } var appointment = qdb.retAppointment(id); appointment.IsConfirmed = true; qdb.upAppointment(appointment); } else { _db.CreditCards.Add(CreditCard); await _db.SaveChangesAsync(); psd = await _db.ProductsSelectedForAppointments.Where(e => e.AppointmentId == id) .ToListAsync(); foreach (var item in psd) { var product = _db.Products.First(e => e.Id == item.ProductId); product.Count -= item.Count; if (product.Count == 0) { product.Available = false; } } _db.Appointments.First(e => e.Id == id).IsConfirmed = true; await _db.SaveChangesAsync(); } Dictionary <int, int> dic = new Dictionary <int, int>(); HttpContext.Session.Set("ls", dic); TempData["congrats"] = 1; return(RedirectToAction("Index", "Home")); }