Esempio n. 1
0
        public async Task <ActionResult> MembersGridViewPartialUpdate([ModelBinder(typeof(DevExpressEditorsBinder))] MurongEnrollment.Models.AspNetUsers item)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var user = await UserManager.FindByIdAsync(item.Id);

                    user.UserName = item.UserName;
                    user.Email    = item.UserName;


                    await UserManager.UpdateAsync(user);

                    var roles = await UserManager.GetRolesAsync(user.Id);

                    await UserManager.RemoveFromRolesAsync(user.Id, roles.ToArray());

                    await UserManager.AddToRoleAsync(user.Id, item.UserRoles);

                    if (item.Password != null && item.Password != "")
                    {
                        await UserManager.ChangePasswordAsync(user, item.Password);
                    }
                }
                catch (Exception e)
                {
                    ViewData["EditError"] = e.Message;
                }
            }
            else
            {
                ViewData["EditError"] = "Please, correct all errors.";
            }
            var model = unitOfWork.UserRepository.Get(includeProperties: "AspNetRoles,AspNetUserDetails");

            return(PartialView("_MembersGridViewPartial", model));
        }
Esempio n. 2
0
        public async Task <ActionResult> MembersGridViewPartialAddNew([ModelBinder(typeof(DevExpressEditorsBinder))] MurongEnrollment.Models.AspNetUsers item)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    item.Id    = Guid.NewGuid().ToString();
                    item.Email = item.UserName;
                    var res = await UserManager.CreateAsync(new Models.Users()
                    {
                        Id = item.Id, Email = item.UserName, UserName = item.UserName
                    }, item.Password);

                    if (res.Succeeded)
                    {
                        await UserManager.AddToRoleAsync(item.Id, item.UserRoles);

                        unitOfWork.AspNetUserDetailsRepo.Insert(new Models.AspNetUserDetails()
                        {
                            UserId = item.Id, MiddleName = item.AspNetUserDetails.MiddleName, LastName = item.AspNetUserDetails.LastName, FirstName = item.AspNetUserDetails.FirstName
                        });
                        unitOfWork.Save();
                    }
                }
                catch (Exception e)
                {
                    ViewData["EditError"] = e.Message;
                }
            }
            else
            {
                ViewData["EditError"] = "Please, correct all errors.";
            }
            var model = unitOfWork.UserRepository.Get(includeProperties: "AspNetRoles,AspNetUserDetails");

            return(PartialView("_MembersGridViewPartial", model));
        }