Beispiel #1
0
        // To protect from overposting attacks, enable the specific properties you want to bind to, for
        // more details, see https://aka.ms/RazorPagesCRUD.
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            _context.Attach(Transaction).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!TransactionExists(Transaction.TransactionId))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(RedirectToPage("./Index"));
        }
Beispiel #2
0
        public async Task <IActionResult> OnPostAsync()
        {
            // CHECK BUILT-IN VALIDATION RULES AND RETURN PAGE IF ERRORS FOUND
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            // DATE OF BIRTH VALIDATION
            var birthYear         = AccountHolder.DateOfBirth.Year;
            var latestAllowedYear = DateTime.Now.Year - 18;

            if (birthYear > latestAllowedYear)
            {
                ModelState.AddModelError("AccountHolder.DateOfBirth", "Account holder must be 18 years or older");
            }

            // SSN VALIDATION
            var  ssn              = AccountHolder.SocialSecurityNumber;
            var  currentId        = AccountHolder.AccountHolderId;
            bool ssnAlreadyExists = await _context.AccountHolder.AnyAsync(x => x.SocialSecurityNumber == ssn && x.AccountHolderId != currentId);

            if (ssnAlreadyExists)
            {
                ModelState.AddModelError("AccountHolder.SocialSecurityNumber", "Account holder with this SSN already exists");
            }

            // CHECK AGAIN AFTER CUSTOM VALIDATION RULES AND RETURN PAGE IF ERRORS FOUND
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            _context.Attach(AccountHolder).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!AccountHolderExists(AccountHolder.AccountHolderId))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(RedirectToPage("./Index"));
        }