示例#1
0
        public async Task <IActionResult> Edit(EditViewModel vm)
        {
            if (!ModelState.IsValid)
            {
                vm.AllRoles = await _rolesDao.GetAllAsync(false);

                return(View(vm));
            }

            var user = new User
            {
                Id        = vm.Id,
                UserName  = vm.UserName,
                Email     = vm.Email,
                FirstName = vm.FirstName,
                LastName  = vm.LastName
            };

            try
            {
                if (vm.EditorMode == EditorMode.Create)
                {
                    var role = await _rolesDao.GetByIdAsync(vm.RoleId);

                    await _usersDao.CreateAsync(
                        user,
                        _configration["Config:Users:DefaultPassword"],
                        role.Name);
                }
                else
                {
                    await _usersDao.UpdateAsync(user, vm.RoleId);
                }
            }
            catch (IdentityException e)
            {
                foreach (var error in e.Errors)
                {
                    ModelState.AddModelError("", error);
                }
            }
            catch (Exception e)
            {
                ModelState.AddModelError("", e.Message);
            }

            if (ModelState.ErrorCount != 0)
            {
                vm.AllRoles = await _rolesDao.GetAllAsync(false);

                return(View(vm));
            }
            else
            {
                return(Redirect(vm.ReturnUrl));
            }
        }