public async Task <IActionResult> Create([Bind("EmployeeId, WorkDay, MorningAttendance, AfternoonAttendance")] AttendanceRecordModel attendanceRecord) { var currentUserId = _userManager.GetUserId(User); var users = await GetUsersInScope(currentUserId); // validate that posted EmployeeId is valid for current user role if (!users.Any(u => u.Id == attendanceRecord.EmployeeId)) { return(Forbid()); } if (_context.AttendanceRecords.AsNoTracking().Any(p => p.EmployeeId == attendanceRecord.EmployeeId && p.WorkDay == attendanceRecord.WorkDay)) { ModelState.AddModelError(string.Empty, "Unable to save this Presence Record. The entry with this work day date already exists. " + "Please select a different date or remove the original entry with the same date first."); PopulateViewDataWithSelectedItems(attendanceRecord); return(View(attendanceRecord)); } if (ModelState.IsValid) { _context.Add(attendanceRecord); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } PopulateViewDataWithSelectedItems(attendanceRecord); return(View(attendanceRecord)); }
/// <summary> /// Helper method: Prepares ViewData and pre-selects last selection /// </summary> /// <param name="attendanceRecord"></param> private void PopulateViewDataWithSelectedItems(AttendanceRecordModel attendanceRecord) { ViewData["EmployeeId"] = new SelectList(_context.Users, "Id", "UserName", attendanceRecord.EmployeeId); ViewData["MorningAttendance"] = new SelectList(Enum.GetNames(typeof(Attendance)), attendanceRecord.MorningAttendance); ViewData["AfternoonAttendance"] = new SelectList(Enum.GetNames(typeof(Attendance)), attendanceRecord.AfternoonAttendance); ViewData["ManagerApprovalStatus"] = new SelectList(Enum.GetNames(typeof(ManagerApprovalStatus)), attendanceRecord.ManagerApprovalStatus); ViewData["ManagerApprovalControlDisabled"] = true; }