public ActionResult Edit(int id) { try { var objUser = db.Users.First(o => o.User_Id == id); var objUserViewModel = new UserEditView() { User_Id = objUser.User_Id, UserName = objUser.UserName, PassWord = string.Empty, Lock = objUser.Lock, Roles = objUser.Roles, Email = objUser.Email, FirstName = objUser.FirstName, FamilyName = objUser.FamilyName, CompanyName = objUser.CompanyName, PhoneNumber = objUser.PhoneNumber, Address = objUser.Address, Remove = objUser.Remove, LstRoles = GetRoles() }; return View("AddAndEditUser", objUserViewModel); } catch (Exception ex) { throw new System.ArgumentException(ex.Message); } }
public ActionResult AddAndEditUser() { var objView = new UserEditView() { LstRoles = GetRoles() }; return View(objView); }
public ActionResult Save(UserEditView obj) { try { Validate(obj); if (ModelState.IsValid) { var objSave = new User(); if (obj.User_Id > 0) { objSave = db.Users.First(o => o.User_Id == obj.User_Id); objSave.User_Id = obj.User_Id; objSave.UserName = obj.UserName; if (!string.IsNullOrEmpty(obj.PassWord)) objSave.PassWord = Hashing.HashPassword(obj.PassWord); objSave.Lock = obj.Lock; objSave.Roles = obj.Roles; objSave.Email = obj.Email; objSave.FirstName = obj.FirstName; objSave.FamilyName = obj.FamilyName; objSave.CompanyName = obj.CompanyName; objSave.PhoneNumber = obj.PhoneNumber; objSave.Address = obj.Address; } else { Mapper.CreateMap<UserEditView, User>(); objSave = Mapper.Map<User>(obj); objSave.PassWord = Hashing.HashPassword(obj.PassWord); } db.Users.Add(objSave); if (obj.User_Id > 0) { db.Entry(objSave).State = EntityState.Modified; } db.SaveChanges(); return RedirectToAction("AllUser"); } obj.LstRoles = GetRoles(); return View("AddAndEditUser", obj); } catch (Exception ex) { throw new System.ArgumentException(ex.Message); } }
private void Validate(UserEditView obj) { ModelState.Clear(); if (string.IsNullOrEmpty(obj.UserName)) ModelState.AddModelError("UserName", "UserName is required !"); if (string.IsNullOrEmpty(obj.Roles)) ModelState.AddModelError("Roles", "Roles is required !"); if (string.IsNullOrEmpty(obj.PassWord) && obj.User_Id == 0) ModelState.AddModelError("PassWord", "PassWord is required !"); }
public ActionResult Register() { var objView = new UserEditView() { LstRoles = new UserController().GetRoles().Where(o=>o.Value != "Admin").ToList() }; return View(objView); }