Example #1
0
        public async Task <IActionResult> OnPostAsync()
        {
            var existingRole = await _context.GetRoleByNameAsync(Role.Name);

            if (existingRole != null)
            {
                ModelState.AddModelError("Name", $"Name is already taken.");
            }
            ;

            if (!ModelState.IsValid)
            {
                return(Page());
            }

            var role = new Role
            {
                Name = Role.Name
            };

            _context.AddRole(role);
            await _context.SaveChangesAsync();

            TempData["Message"] = $"The role, {role.Name}, was successfully added.";

            return(RedirectToPage("./Index"));
        }
Example #2
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            List <ImportRoleModel> importedRoles;

            try
            {
                importedRoles = JsonSerializer.Deserialize <List <ImportRoleModel> >(Json);
            }
            catch
            {
                ModelState.AddModelError("Json", "Error reading JSON.");
                return(Page());
            }

            foreach (var role in importedRoles)
            {
                var entity = new Entities.Role();
                entity.Name = role.Name;

                _context.AddRole(entity);
            }

            try
            {
                await _context.SaveChangesAsync();

                TempData["Message"] = $"{importedRoles.Count} role(s) were successfully imported.";
                return(RedirectToPage("./Index"));
            }
            catch
            {
                ModelState.AddModelError("Json", "Error saving roles to database.");
            }

            return(Page());
        }