public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            SimpleBillPay.Models.BillPay billPay = await _billPayService.GetByIdAsync(BillPay.ID);

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

            billPay.StartingAmount = BillPay.StartingAmount;
            billPay.BillPayDate    = BillPay.BillPayDate;

            try
            {
                await _billPayService.UpdateBillPayAsync(billPay);
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!await _billPayService.BillPayExistsAsync(BillPay.ID))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }
            catch (DbUpdateException ex)
            {
                if (ex.InnerException is MySqlException)
                {
                    if (((MySqlException)ex.InnerException).Number == 1062)
                    {
                        return(RedirectToPage("./Edit",
                                              new
                        {
                            id = BillPay.ID,
                            duplicateDateError = BillPay.BillPayDate.ToString("yyyy-MM-dd")
                        }));
                    }
                }
            }
            return(RedirectToPage("./Edit", new { id = BillPay.ID }));
        }
        public async Task <IActionResult> OnGetAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            BillPay = await _billPayService.GetByIdAsync((int)id);

            if (BillPay == null)
            {
                return(NotFound());
            }
            return(Page());
        }
        public async Task <IActionResult> OnPostAddPayment(int?billInstanceID, int?billPayID)
        {
            if (billPayID == null || billInstanceID == null)
            {
                return(NotFound());
            }

            //Ensure parameters belong to current user
            BillInstance instance = await _billService.GetBillInstanceAsync((int)billInstanceID);

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

            SimpleBillPay.Models.BillPay billPay = await _billPayService.GetByIdAsync((int)billPayID);

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

            //Add new payment with this bill instance and attached to this bill pay
            Payment payment = new Payment();

            payment.BillInstance = instance;

            payment.Amount        = (instance.Amount - instance.Payments.Sum(p => p.Amount));
            payment.PaymentDate   = billPay.BillPayDate;
            payment.DateConfirmed = null;
            payment.BillPay       = billPay;

            await _paymentService.AddAsync(payment);

            //Call get method to rebuild page
            return(Redirect("./Edit?id=" + billPayID.ToString()));
        }