public ActionResult EditUser(tb_UserInfo user) { tb_UserInfo aUser = dbContext.tb_UserInfos.FirstOrDefault(x => x.UserInfoId == user.UserInfoId); try { if (aUser != null) { aUser.UserInfoId = user.UserInfoId; } var unique = dbContext.tb_UserInfos.FirstOrDefault(x => x.Email == user.Email && x.UserInfoId != user.UserInfoId); if (unique != null) { ModelState.AddModelError("", "Email Already exsist."); return(RedirectToAction("EditUser", "Home", new{ userId = user.UserInfoId })); } else { // to remove same key already exists in the ObjectStateManager ((IObjectContextAdapter)dbContext).ObjectContext.Detach(aUser); dbContext.Entry(user).State = EntityState.Modified; dbContext.SaveChanges(); } } catch (Exception) { return(RedirectToAction("UserList")); } return(RedirectToAction("UserList")); }
public ActionResult DeleteUser(int userId) { tb_UserInfo aUser = dbContext.tb_UserInfos.FirstOrDefault(x => x.UserInfoId == userId); if (aUser != null) { dbContext.tb_UserInfos.Remove(aUser); dbContext.SaveChanges(); } return(RedirectToAction("UserList")); }
public ActionResult UserInfo(tb_UserInfo user) { try { var unique = dbContext.tb_UserInfos.FirstOrDefault(x => x.Email == user.Email); if (unique != null) { return(RedirectToAction("UserInfo", new{ mail = true })); } dbContext.tb_UserInfos.Add(user); dbContext.SaveChanges(); //ViewBag.Massage = "Saved"; } catch (Exception) { //ViewBag.Massage = "Try Again"; return(RedirectToAction("UserInfo")); } return(RedirectToAction("UserInfo")); }
/// <summary> /// 添加一条用户信息 /// </summary> private void AddUserInfo(UserInfo userInfo) { var newtbUserInfo = new tb_UserInfo() { Code = Guid.NewGuid().ToString(), UserName = userInfo.UserName, Password = userInfo.Password, Sex = userInfo.Sex, Email = userInfo.Email, Mark = 0, RewardMark = 0, PaidMark = 0, ACount = 0, AAcceptCount = 0, QSolvedCount = 0, QUnsolveCount = 0, QCancelledCount = 0, CreatedDate = userInfo.CreatedDate }; _baseDbContext.tb_UserInfo.Add(newtbUserInfo); _baseDbContext.SaveChanges(); }