public ActionResult UpdateRole(RoleModelView roleModel, string id)
        {
            if (ModelState.IsValid)
            {
                var roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(new ApplicationDbContext()));

                if (!roleManager.RoleExists(roleModel.RoleName))
                {
                    var role = roleManager.FindById(id);
                    role.Name = roleModel.RoleName;
                    var result = roleManager.Update(role);

                    if (result.Succeeded)
                    {
                        //ModelState.AddModelError("", "Role Added Successfully");
                        return(RedirectToAction("AllRoles"));
                    }
                    else
                    {
                        foreach (var item in result.Errors)
                        {
                            ModelState.AddModelError("", item);
                        }
                    }
                }
                else
                {
                    ModelState.AddModelError("", "There is another Role has the same Name");
                }
            }

            return(View(roleModel));
        }
Ejemplo n.º 2
0
        public ActionResult CreateRole(RoleModelView obj)
        {
            RoleLogic rl = new RoleLogic();

            rl.AddRole(obj.Name);
            return(RedirectToAction("GetallRole"));
        }
        public async Task <IActionResult> CreateRole(RoleModelView role)
        {
            if (ModelState.IsValid)
            {
                IdentityRole   identity = new IdentityRole(role.RoleName);
                IdentityResult result   = await roleManager.CreateAsync(identity);

                if (result.Succeeded)
                {
                    return(RedirectToAction("ListRoles", "Administration"));
                }
                foreach (var item in result.Errors)
                {
                    ModelState.AddModelError("", item.Description);
                }
            }
            return(View(role));
        }
        public ActionResult AddRole(RoleModelView roleModel)
        {
            var roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(new ApplicationDbContext()));

            if (ModelState.IsValid)
            {
                if (!roleManager.RoleExists(roleModel.RoleName))
                {
                    var role = new IdentityRole
                    {
                        Name = roleModel.RoleName
                    };
                    var result = roleManager.Create(role);

                    if (result.Succeeded)
                    {
                        //ModelState.AddModelError("", "Role Added Successfully");
                        return(RedirectToAction("AllRoles"));
                    }
                    else
                    {
                        foreach (var item in result.Errors)
                        {
                            ModelState.AddModelError("", item);
                        }
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Role Already Exists");
                }
            }
            else
            {
                ModelState.AddModelError("", "You Have Some Errors.\nPlease, Fix it.");
            }

            return(View(roleModel));
        }