public ActionResult Create(RegisterModel model) { ViewBag.Des = "新增操作员"; var roles = _service.GetAllUserRoles().TakeWhile(n => n.SystemName != SystemUserRoleNames.Administrators); ViewBag.Roles = new SelectList(roles, "Id", "Name"); User newuser; if (model.Id == 0) { var user = new User { UserGuid = Guid.NewGuid(), Username = model.UserName, Email = model.Email, Mobile = model.Mobile, Active = true, //加密存储 Password = Encrypt.EncryptString(model.Password), }; //默认增加注册角色 // 先插入 _service.InsertUser(user); newuser = _service.GetUserByUsername(user.Username); } else { newuser = _service.GetUserById(model.Id); newuser.Username = model.UserName; newuser.Password = Encrypt.EncryptString(model.Password); newuser.Email = model.Email; newuser.Mobile = model.Mobile; ViewBag.Des = "编辑用户"; } var role = _service.GetUserRoleById(model.RoleId); //先只有一个角色 newuser.UserRoles.Clear(); newuser.UserRoles.Add(role); try { _service.UpdateUser(newuser); Success(); model.Empty(); } catch (Exception e) { Error(e.Message); } return(View(model)); }