public async Task <IActionResult> Edit(string studentId, string courseCode, [Bind("CourseCode,StudentId,Grade")] AcademicRecord academicRecord)
        {
            if (studentId != academicRecord.StudentId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(academicRecord);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!AcademicRecordExists(academicRecord.StudentId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }

            // Pass student name and course title to view
            populateEditViewData(studentId, courseCode);

            return(View(academicRecord));
        }
Exemplo n.º 2
0
 public async Task <IActionResult> Edit([Bind("CourseCode,StudentId,Grade")] AcademicRecord academicRecord)
 {
     if (ModelState.IsValid)
     {
         try
         {
             _context.Update(academicRecord);
             await _context.SaveChangesAsync();
         }
         catch (DbUpdateConcurrencyException)
         {
             if (!AcademicRecordExists(academicRecord.StudentId))
             {
                 return(NotFound());
             }
             else
             {
                 throw;
             }
         }
         return(RedirectToAction(nameof(Index)));
     }
     ViewData["CourseCode"] = new SelectList(_context.Course, "Code", "Code", academicRecord.CourseCode);
     ViewData["StudentId"]  = new SelectList(_context.Student, "Id", "Id", academicRecord.StudentId);
     return(View(academicRecord));
 }
        public async Task <IActionResult> Index(string sort, [Bind("CourseCode[], StudentId[], Grade[]")] AcademicRecord[] academicRecord)
        {
            if (ModelState.IsValid)
            {
                for (int i = 0; i < academicRecord.Length; i++)
                {
                    try
                    {
                        //save the student's academic record
                        _context.Update(academicRecord[i]);
                        await _context.SaveChangesAsync();
                    }
                    catch
                    {
                        //if it fails, continue onto the next student
                        continue;
                    }
                }
            }

            ViewData["sort"] = sort; //set the last sorting used

            //creating array from db to modify index view:
            var StudentRecordContext = _context.AcademicRecord.Include(a => a.CourseCodeNavigation).Include(a => a.Student);

            AcademicRecord[] records       = StudentRecordContext.ToArray();
            AcademicRecord[] sortedRecords = records;

            //Re-sort the data using the last sort method the user selected (stored in the ViewData["sort"] variable)
            if (sort == "course")
            {
                sortedRecords = records.OrderBy(r => r.CourseCodeNavigation.Title).ToArray();
            }
            else //from here down, updated for lab9
            {
                sortedRecords = records.OrderBy(r => r.Student.Name).ToArray();
            }
            if (HttpContext.Session.GetString("SortOrder") == null ||
                HttpContext.Session.GetString("SortOrder") == "Descending")
            {
                HttpContext.Session.SetString("SortOrder", "Ascending");
            }
            else
            {
                HttpContext.Session.SetString("SortOrder", "Descending");
                sortedRecords = sortedRecords.Reverse().ToArray();
            }
            return(View(sortedRecords));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Edit(string id, [Bind("Code,Title,Description,HoursPerWeek,FeeBase")] Course course)
        {
            if (id != course.Code)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(course);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CourseExists(course.Code))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(course));
        }
        public async Task <IActionResult> Edit(int id, [Bind("Id,Name,UserName,Password")] Employee employee)
        {
            if (id != employee.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(employee);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!EmployeeExists(employee.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(employee));
        }
        public async Task <IActionResult> Edit(string id, [Bind("Id,Name")] Student student)
        {
            if (id != student.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(student);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!StudentExists(student.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(student));
        }
Exemplo n.º 7
0
        public async Task <IActionResult> Edit(EmployeeRoleSelections employeeRoleSelections)
        {
            if (!employeeRoleSelections.roleSelections.Any(m => m.Selected))
            { // Validate RoleSelections
                ModelState.AddModelError("roleSelections", "You must select at least one role!");
            }
            if (_context.Employee.Any(e => e.UserName == employeeRoleSelections.employee.UserName &&
                                      e.Id != employeeRoleSelections.employee.Id))
            { // Validate user name
                ModelState.AddModelError("employee.UserName", "This user name already exists!");
            }
            if (ModelState.IsValid)
            { // Update DB context:
                try
                {
                    _context.Update(employeeRoleSelections.employee);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!EmployeeExists(employeeRoleSelections.employee.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }

                // Reset EmployeeRoles
                var ERTable = _context.EmployeeRole
                              .Where(er => er.EmployeeId == employeeRoleSelections.employee.Id);
                foreach (var roleRow in ERTable)
                {
                    _context.EmployeeRole.Remove(roleRow);
                }
                foreach (RoleSelection roleSelection in employeeRoleSelections.roleSelections)
                { // Add EmployeeRoles
                    if (roleSelection.Selected)
                    {
                        EmployeeRole employeeRole = new EmployeeRole
                        {
                            RoleId     = roleSelection.role.Id,
                            EmployeeId = employeeRoleSelections.employee.Id
                        };
                        _context.EmployeeRole.Add(employeeRole);
                    }
                }
                _context.SaveChanges();
                return(RedirectToAction(nameof(Index)));
            }
            return(View(employeeRoleSelections));
        }
        public async Task <IActionResult> Edit(int id, EmployeeRoleSelections employeeRoleSelections)
        {
            if (!employeeRoleSelections.roleSelections.Any(mbox => mbox.Selected))
            {
                ModelState.AddModelError("roleSelections", "You must select one role!");
            }
            if (id != employeeRoleSelections.employee.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(employeeRoleSelections.employee);
                    _context.SaveChanges();

                    var rolesSelected = _context.EmployeeRole.Where(e => e.EmployeeId == employeeRoleSelections.employee.Id).ToList();
                    _context.RemoveRange(rolesSelected);

                    foreach (RoleSelection roleSelection in employeeRoleSelections.roleSelections)
                    {
                        if (roleSelection.Selected)
                        {
                            EmployeeRole employeeRole = new EmployeeRole
                            {
                                RoleId     = roleSelection.role.Id,
                                EmployeeId = employeeRoleSelections.employee.Id
                            };
                            _context.EmployeeRole.Add(employeeRole);
                        }
                    }
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!EmployeeExists(employeeRoleSelections.employee.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                _context.SaveChanges();
                return(RedirectToAction(nameof(Index)));
            }
            return(View(employeeRoleSelections));
        }
Exemplo n.º 9
0
        public async Task <IActionResult> Edit(AcademicRecord[] academicRecords)
        {
            if (ModelState.IsValid)
            {
                foreach (AcademicRecord record in academicRecords)
                {
                    _context.Update(record);
                    await _context.SaveChangesAsync();
                }

                return(RedirectToAction("Edit"));
            }

            return(View(academicRecords));
        }
Exemplo n.º 10
0
        public async Task <IActionResult> Edit(int id, EmployeeRoleSelections employeeRoleSelections)
        {
            if (!employeeRoleSelections.roleSelections.Any(m => m.Selected))
            {
                ModelState.AddModelError("roleSelections", "You must select at least one role!");
            }

            if (ModelState.IsValid)
            {
                try
                {
                    var allEmployeeRoles = _context.EmployeeRole.Where(e => e.EmployeeId == id).ToList();
                    if (allEmployeeRoles.Count > 0)
                    {
                        _context.RemoveRange(allEmployeeRoles);
                    }

                    foreach (RoleSelection roleSelection in employeeRoleSelections.roleSelections)
                    {
                        if (roleSelection.Selected)
                        {
                            _context.EmployeeRole.Add(new EmployeeRole {
                                EmployeeId = id, RoleId = roleSelection.role.Id
                            });
                        }
                    }

                    _context.Update(employeeRoleSelections.employee);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    throw;
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(employeeRoleSelections));
        }