Пример #1
0
        public async Task <IActionResult> OnPostSaveAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

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

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException e)
            {
                //Handle deleted row
                var exceptionEntry = e.Entries.Single();
                var databaseEntry  = exceptionEntry.GetDatabaseValues();
                if (databaseEntry == null)
                {
                    ModelState.AddModelError(string.Empty, "Unable to save. " +
                                             "The contact entity was deleted by another user.");
                    return(Page());
                }

                ModelState.AddModelError(string.Empty, "Unable to save. " +
                                         "The contact entity was changed by another user.");
                return(Page());
            }

            return(RedirectToPage("/Contact/Index"));
        }
        public async Task <IActionResult> Edit(int id, [Bind("ContactId,OwnerID,Name,Address,City,State,Zip,Email,Status")] Contact contact)
        {
            if (id != contact.ContactId)
            {
                return(NotFound());
            }

            //if (ModelState.IsValid)
            //{
            //    try
            //    {
            //        _context.Update(contact);
            //        await _context.SaveChangesAsync();
            //    }
            //    catch (DbUpdateConcurrencyException)
            //    {
            //        if (!ContactExists(contact.ContactId))
            //        {
            //            return NotFound();
            //        }
            //        else
            //        {
            //            throw;
            //        }
            //    }
            //    return RedirectToAction(nameof(Index));
            //}
            //-------------------------------------------
            if (ModelState.IsValid)
            {
                var contacts = await _context.Contacts.AsNoTracking()
                               .FirstOrDefaultAsync(m => m.ContactId == id);

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

                var isAuthorized = await _authorizationService.AuthorizeAsync(
                    User, contact,
                    ContactOperations.Update);

                if (!isAuthorized.Succeeded)
                {
                    return(Forbid());
                }

                Contact.OwnerID = contact.OwnerID;

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

                if (Contact.Status == ContactStatus.Approved)
                {
                    // If the contact is updated after approval,
                    // and the user cannot approve,
                    // set the status back to submitted so the update can be
                    // checked and approved.
                    var canApprove = await _authorizationService.AuthorizeAsync(User,
                                                                                Contacts,
                                                                                ContactOperations.Approve);

                    if (!canApprove.Succeeded)
                    {
                        Contact.Status = ContactStatus.Submitted;
                    }
                }

                await _context.SaveChangesAsync();

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

            //-----------------------------------------------

            return(View(contact));
        }