public ActionResult User(string logonName, int? pageIndex) { var vm = new SysUserViewModel(); if (pageIndex.HasValue) vm.PageIndex = pageIndex.Value; var list = _uow.SysUserService.GetQueryList(); if (!string.IsNullOrEmpty(logonName)) { list = list.Where(t => t.LogonName.Contains(logonName)); } vm.SysUserList = new Common.Helper.PageList<SysUser>(list.OrderBy(t => t.Id), vm.PageIndex, vm.PageCount); vm.SubMenuList = GetSubMenuList(); return View(vm); }
public ActionResult EditUser(int? id) { var vm = new SysUserViewModel(); vm.SubMenuList = GetSubMenuList(); vm.UserModel = new SysUser(); vm.UserModel.Password = WebUtils.GetAppSettingValue(StaticKey.AppDefaultUserPwd).ToMd5(); vm.HaveRoles = new HashSet<int>(); vm.RoleList = _uow.SysRoleService.GetQueryList().ToList(); if (id.HasValue) { vm.UserModel = _uow.SysUserService.Get(t => t.Id == id.Value); vm.HaveRoles = _uow.RelUserRoleService.GetQueryList(t => t.UserId == id.Value).Select(t => t.RoleId).ToHashSet(); } return View(vm); }
public ActionResult EditUser(SysUserViewModel model) { if (ModelState.IsValid) { if (model.UserModel.Id == 0) { model.UserModel.Password = WebUtils.GetAppSettingValue(StaticKey.AppDefaultUserPwd).ToMd5(); _uow.SysUserService.Add(model.UserModel); _uow.Commit(); if (model.HaveRoles != null) { foreach (var item in model.HaveRoles) { _uow.RelUserRoleService.Add(new RelUserRole() { UserId = model.UserModel.Id, RoleId = item }); } } } else { var old = _uow.SysUserService.Get(t => t.Id == model.UserModel.Id); if (old != null) { old.LogonName = model.UserModel.LogonName; old.TrueName = model.UserModel.TrueName; old.Phone = model.UserModel.Phone; old.Email = model.UserModel.Email; //删除角色 var urList = _uow.RelUserRoleService.GetQueryList(t => t.UserId == old.Id); foreach (var item in urList) { _uow.RelUserRoleService.Delete(item); } //新加角色 if (model.HaveRoles != null) { foreach (var item in model.HaveRoles) { _uow.RelUserRoleService.Add(new RelUserRole() { UserId = model.UserModel.Id, RoleId = item }); } } } } _uow.Commit(); } return RedirectToAction("User"); }