示例#1
0
        public async Task <IActionResult> Edit(int id, Staff staff)
        {
            if (id != staff.idStaff)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    await staffBusiness.updateStaff(staff);

                    var user = await _userManager.FindByNameAsync(staff.adEmail);

                    if (staff.isAdmin)
                    {
                        if (!await _userManager.IsInRoleAsync(user, "Admin"))
                        {
                            await _userManager.AddToRoleAsync(user, "Admin");
                        }
                    }
                    else
                    {
                        if (await _userManager.IsInRoleAsync(user, "Admin"))
                        {
                            await _userManager.RemoveFromRoleAsync(user, "Admin");
                        }
                    }
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!await StaffExists(staff.idStaff))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            await ViewDataAddressesAndStores();

            return(View(staff));
        }