public IActionResult Create() { var model = new DDCreateUserViewModel(); model.Groups = LoadGroups(); return(View(model)); }
public async Task <IActionResult> Create(DDCreateUserViewModel model) { if (!ModelState.IsValid) { return(View(model)); } if (model.GroupId == "-1") { ModelState.AddModelError(string.Empty, "必须选择集团"); } //创建用户 var user = new ApplicationUser { UserName = model.UserName, GroupId = model.GroupId, ProjectIds = model.ProjectId }; var createUserResult = await _userManager.CreateAsync(user, model.Password); if (!createUserResult.Succeeded) { ModelState.AddModelError(string.Empty, "创建用户失败"); } //创建角色 //默认用户的角色是 DianGong var createRoleResult = await _userManager.AddToRoleAsync(user, "DianGong"); if (!createRoleResult.Succeeded) { ModelState.AddModelError(string.Empty, "创建用户角色失败"); } if (ModelState.ErrorCount > 0) { return(View(model)); } return(RedirectToAction(nameof(Index))); }