public bool Create(UserSingleModel model) { using (var db = new EntityContext()) { var user = new TbUser { Username = model.Username, Password = model.Password, Status = db.tbStatuses.FirstOrDefault(s => s.Status == model.Status), Role = db.tbRoles.FirstOrDefault(r => r.Role == model.Role), Phonenumber = model.Phonenumber, isDelete = false }; db.tbUsers.Add(user); db.SaveChanges(); return(true); } }
public bool Edit(UserSingleModel model) { using (var db = new EntityContext()) { var user = userUtility.GetSingleUser(model.Id ?? default(int), db); if (user != null) { db.Entry(user).State = EntityState.Modified; user.Password = model.Password; user.Phonenumber = model.Phonenumber; user.Role = db.tbRoles.FirstOrDefault(x => x.Role == model.Role); user.Status = db.tbStatuses.FirstOrDefault(x => x.Status == model.Status); user.Username = model.Username; db.SaveChanges(); return(true); } else { return(false); } } }
public ActionResult CreateUpdateAccount(UserSingleModel model) { AccountDropdownlist(); if (ModelState.IsValid) { if (model.Id > 0) { bool result = userService.Edit(model); if (result == true) { ViewBag.Message = "ສຳເລັດ"; return(View(model)); } else { ViewBag.Message = "ບໍ່ສຳເລັດ ກະລຸນາລອງໃຫມ່ອີກຄັ້ງ"; return(View(model)); } } else { bool result = userService.Create(model); if (result == true) { ViewBag.Message = "ສຳເລັດ"; return(View()); } else { ViewBag.Message = "ບໍ່ສຳເລັດ ກະລຸນາລອງໃຫມ່ອີກຄັ້ງ"; return(View(model)); } } } else { return(View()); } }
public UserSingleModel GetSingle(int id) { using (var db = new EntityContext()) { var user = userUtility.GetSingleUser(id, db); if (user != null) { var model = new UserSingleModel { Id = user.Id, Status = user.Status.Status, Password = user.Password, Phonenumber = user.Phonenumber, Role = user.Role.Role, Username = user.Username }; return(model); } else { return(null); } } }