public ActionResult StaffPositions(UserInfoViewModel model, string buttonClicked, ProgramSelectItem filterModel, List <int> selection, int levelId = 0) { if (model.User.Id > 0) { var user = _coreRepo.Get <User>(model.User.Id); Instructor instructor = _staffRepo.GetStaffByUserId(model.User.Id); model.User = user; model.StaffPositions = instructor != null?instructor.InstructorPositions.ToList() : null; if (buttonClicked == "Add Position") { model.StaffPosition = new InstructorPosition() { Position = new Position() }; ViewBag.Positions = _coreRepo.GetAll <Position>(); return(PartialView("_addStaffPosition", model)); } if (buttonClicked == "Save") { //Saving Staff Position if (levelId > 0) { if (levelId == (int)AccessLevel.Campus) { if (filterModel.CampusId <= 0) { ModelState.AddModelError("Error", "Kindly select a Campus!"); ViewBag.Positions = _coreRepo.GetAll <Position>(); model.StaffPosition.LevelId = AccessLevel.Campus; return(PartialView("_addStaffPosition", model)); } } //if (levelId == (int)AccessLevel.Faculty) //{ // if (filterModel.FacultyId <= 0) // { // ModelState.AddModelError("Error", "Kindly select a Faculty!"); // ViewBag.Positions = _coreRepo.GetAll<Position>(); // model.StaffPosition.LevelId = AccessLevel.Faculty; // return PartialView("_addStaffPosition", model); // } //} //if (levelId == (int)AccessLevel.Department) //{ // if (filterModel.DepartmentId <= 0) // { // ModelState.AddModelError("Error", "Kindly select a Department!"); // ViewBag.Positions = _coreRepo.GetAll<Position>(); // model.StaffPosition.LevelId = AccessLevel.Department; // return PartialView("_addStaffPosition", model); // } //} //if (levelId == (int)AccessLevel.Program) //{ // if (filterModel.ProgramId <= 0) // { // ModelState.AddModelError("Error", "Kindly select a Program!"); // ViewBag.Positions = _coreRepo.GetAll<Position>(); // model.StaffPosition.LevelId = AccessLevel.Program; // return PartialView("_addStaffPosition", model); // } //} //if (levelId == (int)AccessLevel.Course && selection != null) //{ // if (!selection.Any()) // { // ModelState.AddModelError("Error", "Kindly select a Course!"); // ViewBag.Positions = _coreRepo.GetAll<Position>(); // model.StaffPosition.LevelId = AccessLevel.Course; // return PartialView("_addStaffPosition", model); // } //} Campus campus = null; if (filterModel.CampusId > 0) { campus = _coreRepo.Get <Campus>(Convert.ToInt32(filterModel.CampusId)); } //if (levelId == (int)AccessLevel.Course && selection != null) //{ // if (selection.Any()) // { // var course = _coreRepo.Get<Course>(selection.FirstOrDefault()); // //campus = program.Campus; // } //} if (model.StaffPosition.Id == 0) { var staffPosition = new InstructorPosition() { AssignedDate = DateTime.Now, IsActive = model.StaffPosition.IsActive, LevelId = (AccessLevel)Convert.ToInt32(levelId), Staff = instructor, IdNumber = model.StaffPosition.IdNumber, WorkStatus = model.StaffPosition.WorkStatus, Position = _coreRepo.Get <Position>(model.StaffPosition.Position.Id), Campus = campus }; int staffpositionId = (int)_coreRepo.Save(staffPosition); //if (staffpositionId > 0 && (AccessLevel)Convert.ToInt32(levelId) == AccessLevel.Course && selection != null // ) //{ // if (selection.Any()) // TempData[ApplicationConstants.SuccessNotification] = // ApplicationConstants.SuccessSaveMessage; // var selectedCourses = _coreRepo.GetAll<Course>(selection); // var newStaffPosition = _coreRepo.Get<InstructorPosition>(staffpositionId); // var newStaffCourses = selectedCourses.Select(course => new StaffCourse() // { // StaffPosition = newStaffPosition, // Course = course // }).ToList(); // if (_coreRepo.SaveOrUpdateAll(newStaffCourses)) // { // TempData[ApplicationConstants.SuccessNotification] = // "Assigned Courses have been submitted"; // } //} } else { var staffPosition = _coreRepo.Get <InstructorPosition>(model.StaffPosition.Id); { staffPosition.AssignedDate = DateTime.Now; staffPosition.IsActive = model.StaffPosition.IsActive; staffPosition.LevelId = (AccessLevel)Convert.ToInt32(levelId); staffPosition.IdNumber = model.StaffPosition.IdNumber; staffPosition.WorkStatus = model.StaffPosition.WorkStatus; staffPosition.Position = _coreRepo.Get <Position>(model.StaffPosition.Position.Id); staffPosition.Campus = campus; staffPosition.Staff = instructor; } //if ((AccessLevel)Convert.ToInt32(levelId) == AccessLevel.Course && selection != null // ) //{ // if (selection.Any()) // //Delete old course assignments // _coreRepo.Delete<StaffCourse>(staff.StaffPositions.SelectMany(a => a.StaffCourses.Where(x => x.StaffPosition.Id == staffPosition.Id).Select(b => b.Id)).ToList<int>()); // foreach (var courseId in selection) // { // staffPosition.StaffCourses.Add(new StaffCourse() { Course = _coreRepo.Get<Course>(courseId) }); // } //} if (_coreRepo.SaveOrUpdate(staffPosition)) {// return View("_StaffPositions", model); TempData[ApplicationConstants.SuccessNotification] = ApplicationConstants.SuccessSaveMessage; } } } else { ModelState.AddModelError("Error", "Kindly select a level for this position!"); ViewBag.Positions = _coreRepo.GetAll <Position>(); return(PartialView("_addStaffPosition", model)); } model.StaffPositions = _coreRepo.GetAll <InstructorPosition>().Where(ress => ress.Staff.Id == instructor.Id).ToList(); return(PartialView("_StaffPositions", model)); } else { return(PartialView("_StaffPositions", model)); } } return(null); //PartialView("_StaffPositions", new UserInfoViewModel()); }