public async Task <ActionResult> Create(IndexRoleViewModel model) { if (ModelState.IsValid) { var role = new IdentityRole(model.CurrentRole.Name); var roleresult = await RoleManager.CreateAsync(role); if (!roleresult.Succeeded) { ModelState.AddModelError("", roleresult.Errors.First()); } else { MenuHelper.ClearAllMenuCache(); this.AddNotification("The role [" + role.Name + "] is added succesffully", NotificationType.SUCCESS); //Write log var activityText = "Create new role device [Name: {0}]"; activityText = string.Format(activityText, model.CurrentRole.Name); WriteActivityLog(activityText, ActivityLogType.CreateRole, model.CurrentRole.Id, TargetObjectType.RolesAdmin); return(RedirectToAction("Index")); } } this.AddNotificationModelStateErrors(ModelState); return(RedirectToAction("Index")); }
//public async Task<IActionResult> EditRole(string id, string name) public async Task <IActionResult> EditRole(IndexRoleViewModel roleEdit) { if (string.IsNullOrEmpty(roleEdit.Id) || string.IsNullOrEmpty(roleEdit.RoleName)) { return(NotFound()); } var role = await _roleManager.FindByIdAsync(roleEdit.Id); if (role == null) { return(NotFound()); } role.Name = roleEdit.RoleName; var result = await _roleManager.UpdateAsync(role); if (result.Succeeded) { return(RedirectToAction("Index")); } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } return(View(role)); }
public ActionResult Index() { var model = new IndexRoleViewModel(RoleManager.Roles); List <IdentityRole> roles = new List <IdentityRole>(); try { roles = new RoleRepository(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString).GetAll().OrderBy(m => m.Name).ToList(); } catch (Exception ex) { this.AddNotification("Failed to get data because: " + ex.ToString(), NotificationType.ERROR); return(View(model)); } model.RoleList = roles; return(View(model)); }
public async Task <IActionResult> Delete(string id) { if (id == null) { return(NotFound()); } var role = await _roleManager.FindByIdAsync(id); if (role == null) { return(NotFound()); } var model = new IndexRoleViewModel() { RoleName = role.Name }; return(View(model)); }
public async Task <IActionResult> EditRole(string id) { if (string.IsNullOrEmpty(id)) { return(NotFound()); } var role = await _roleManager.FindByIdAsync(id); if (role == null) { return(NotFound()); } IndexRoleViewModel customRole = new IndexRoleViewModel() { Id = role.Id, RoleName = role.Name }; return(View(customRole)); }
public async Task <IActionResult> AddRole(IndexRoleViewModel roleInsert) { if (string.IsNullOrEmpty(roleInsert.RoleName)) { return(NotFound()); } var role = new IdentityRole(roleInsert.RoleName); // ایجاد نمونه جدید از آبجکت گروه var result = await _roleManager.CreateAsync(role); if (result.Succeeded) { return(RedirectToAction("Index")); } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } return(View(role)); }
public ViewResult Index(int page = 1) { try { IndexRoleViewModel model = new IndexRoleViewModel { RolesList = _roleLogic.List() .OrderBy(r => r.Id) .Skip((page - 1) * PageSize) .Take(PageSize), PagingInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = _roleLogic.List().Count } }; return(View(model)); } catch (ConnectionException ex) { _logger.Log(LogLevel.Error, $"The following error occurred: {ex.Message} @ {GetType().Name}"); ViewBag.ErrorMessage = ex.Message; } catch (NullReferenceException ex) { _logger.Log(LogLevel.Error, $"The following error occurred: {ex.Message} @ {GetType().Name}"); ViewBag.ErrorMessage = ex.Message; } catch (Exception ex) { _logger.Log(LogLevel.Error, $"A general exception occurred: {ex.Message} @ {GetType().Name}"); ViewBag.ErrorMessage = ex.Message; } return(View(null)); }