public CreateUserViewModel() { SelectedPositionId = -1; SelectedRoomId = -1; Positions = db.UserPositionDropdown(); Rooms = db.RoomDropdown(); }
public EditUserViewModel(string id) { var user = db.Users.Find(id); Id = user.Id; FirstName = user.FirstName; LastName = user.LastName; Email = user.Email; IsAdmin = user.IsAdmin; SelectedPositionId = user.UserPositionId.HasValue ? user.UserPositionId.Value : -1; SelectedRoomId = user.RoomId.HasValue ? user.RoomId.Value : -1; Rooms = db.RoomDropdown(); Positions = db.UserPositionDropdown(); }
public ActionResult EditUser(EditUserViewModel vm) { var Success = false; var Message = ""; var firstNameValidation = Models.User.Validator.ValidateFirstName(vm.FirstName); if (firstNameValidation != null) { ModelState.AddModelError("FirstName", firstNameValidation); } var lastNameValidation = Models.User.Validator.ValidateLastName(vm.LastName); if (lastNameValidation != null) { ModelState.AddModelError("LastName", lastNameValidation); } var emailValidation = Models.User.Validator.ValidateEmail(vm.Id, vm.Email); if (emailValidation != null) { ModelState.AddModelError("Email", emailValidation); } var positionValidation = Models.User.Validator.ValidatePosition(vm.SelectedPositionId); if (positionValidation != null) { ModelState.AddModelError("SelectedPositionId", positionValidation); } var roomValidation = Models.User.Validator.ValidateRoom(vm.SelectedRoomId); if (roomValidation != null) { ModelState.AddModelError("SelectedRoomId", roomValidation); } if (ModelState.IsValid) { var user = db.Users.Find(vm.Id); user.FirstName = vm.FirstName; user.LastName = vm.LastName; user.Email = user.UserName = vm.Email; user.IsAdmin = vm.IsAdmin; user.RoomId = vm.SelectedRoomId; user.UserPositionId = vm.SelectedPositionId; try { db.SaveChanges(); Success = true; Message = Models.User.SAVE_SUCCESS; } catch (Exception) { Message = Models.User.SAVE_FAIL; } return(Json(new { Success, Message })); } vm.Rooms = db.RoomDropdown(); vm.Positions = db.UserPositionDropdown(); return(PartialView("_EditUser", vm)); }