示例#1
0
        public async Task <ActionResult> Create(EditUserViewModel dUser)
        {
            if (ModelState.IsValid)
            {
                var result = await UserManager.CreateAsync(dUser.GetUser(), dUser.Password);

                if (result.Succeeded)
                {
                    var user = dUser.GetUser();
                    if (dUser.Role == "admin")
                    {
                        var roles = UserManager.GetRoles(user.Id);
                        foreach (var r in roles)
                        {
                            UserManager.RemoveFromRole(user.Id, r);
                        }
                        UserManager.AddToRole(user.Id, dUser.Role);
                    }

                    if (dUser.Role == "parent")
                    {
                        var roles = UserManager.GetRoles(user.Id);
                        foreach (var r in roles)
                        {
                            UserManager.RemoveFromRole(user.Id, r);
                        }
                        UserManager.AddToRole(user.Id, dUser.Role);
                    }

                    if (dUser.Role == "children")
                    {
                        var roles = UserManager.GetRoles(user.Id);
                        foreach (var r in roles)
                        {
                            UserManager.RemoveFromRole(user.Id, r);
                        }
                        UserManager.AddToRole(user.Id, dUser.Role);
                        ChildrenData data = new ChildrenData();
                        data.ChildrenId    = user.Id;
                        data.ParentId      = dUser.ParentId;
                        data.SchoolClassId = dUser.ClassId;
                        context.ChildrenData.Add(data);
                    }

                    if (dUser.Role == "teacher")
                    {
                        var roles = UserManager.GetRoles(user.Id);
                        foreach (var r in roles)
                        {
                            UserManager.RemoveFromRole(user.Id, r);
                        }
                        UserManager.AddToRole(user.Id, dUser.Role);
                    }

                    context.SaveChanges();
                    return(RedirectToAction("Details", new { id = dUser.GetUser().Id }));
                }
            }
            return(HttpNotFound());
        }