public ActionResult EditUser(string id) { if (String.IsNullOrWhiteSpace(id)) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } AspNetUsers GetUserDb = db.AspNetUsers.Find(id); if (GetUserDb == null) { return(HttpNotFound()); } var GetRoles = db.AspNetRoles.ToList(); var UserID = id; var userRoles = db.AspNetRoles.Include(r => r.AspNetUsers).ToList(); var userRoleNames = (from r in userRoles from u in r.AspNetUsers where u.Id == UserID select r.Name).ToList(); var userRoleId = (from r in userRoles from u in r.AspNetUsers where u.Id == UserID select r.Id).ToList(); Stuff stuff = db.Stuff.Where(a => a.stfId == id).SingleOrDefault(); ViewEditUser userviewmodel = new ViewEditUser { User = GetUserDb, Roles = userRoles, UserRoleNames = userRoleNames, RoleId = userRoleId[0] }; //usermodel.UserRoleNames = userRoleNames; //sermodel.Roles = GetRoles; return(View(userviewmodel)); }
public async Task <PageModel <object> > Edit(ViewEditUser model) { var pageModel = new PageModel <object>(); if (model.userName.IsNullOrEmpty()) { pageModel.state = 30002; pageModel.msg = "请填写姓名"; return(pageModel); } if (model.password.IsNullOrEmpty()) { pageModel.state = 30002; pageModel.msg = "请填写密码"; return(pageModel); } var userdata = await dbsysUserInfoIServices.QueryById(model.id); if (userdata.Password != model.password) { model.password = MD5Helper.MD5Encrypt32(model.password); } if (model.roleId.IsNullOrEmpty()) { pageModel.state = 30002; pageModel.msg = "请填选择角色"; return(pageModel); } try { unitOfWork.BeginTranReadUncommitted(); bool msg = await dbsysUserInfoIServices.Update(new sysUserInfoEntity() { Id = model.id, Age = model.age, Email = model.email, isDeleted = model.isDeleted, LoginName = userdata.LoginName, Password = model.password, Phone = model.phone, Sex = model.sex, UserName = model.userName }); if (!msg) { throw new Exception("修改用户失败"); } int[] roleidarr = model.roleId.ToArray(); // 先删除现有的用户角色 await userRoleIServices.DeleteByWhere(c => c.UserID == model.id); List <UserRoleEntity> list = new List <UserRoleEntity>(); for (int i = 0; i < roleidarr.Length; i++) { list.Add(new UserRoleEntity() { RoleID = roleidarr[i], UserID = model.id }); } int roleid = await userRoleIServices.Add(list); if (roleid == 0) { throw new Exception("修改用户失败"); } unitOfWork.CommitTranUncommitted(); } catch (Exception ex) { unitOfWork.RollbackTranUncommitted(); pageModel.state = 30002; pageModel.msg = ex.Message; return(pageModel); } return(pageModel); }