public async Task <IActionResult> Create([Bind("CourseCode,StudentId,Grade")] AcademicRecord academicRecord)
        {
            var existing = _context.AcademicRecord.Where(a => a.CourseCode.Equals(academicRecord.CourseCode) && a.StudentId.Equals(academicRecord.StudentId)).FirstOrDefault();

            ViewData["Error"] = "";
            if (existing != null)
            {
                ViewData["Error"] = "The student has already had this academic record";
            }
            else
            {
                if (ModelState.IsValid)
                {
                    _context.Add(academicRecord);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
            }
            //ViewData["CourseCode"] = new SelectList(_context.Course, "Code", "Code", academicRecord.CourseCode);
            //ViewData["StudentId"] = new SelectList(_context.Student, "Id", "Id", academicRecord.StudentId);
            ViewData["CourseCode"] = new SelectList(_context.Course.Select(c => new { Code = c.Code, Course = c.Code + " - " + c.Title }), "Code", "Course");
            ViewData["StudentId"]  = new SelectList(_context.Student.Select(s => new { Id = s.Id, Student = s.Id + " - " + s.Name }), "Id", "Student");
            return(View(academicRecord));
        }
 public async Task <IActionResult> Create(EmployeeRoleSelections employeeRoleSelections)
 {
     if (!employeeRoleSelections.roleSelections.Any(m => m.Selected))
     {
         ModelState.AddModelError("roleSelections", "You must select at least one role!");
     }
     if (_context.Employee.Any(e => e.UserName == employeeRoleSelections.employee.UserName))
     {
         ModelState.AddModelError("employee.UserName", "This user name already exists!");
     }
     if (ModelState.IsValid)
     {
         _context.Add(employeeRoleSelections.employee);
         _context.SaveChanges();
         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);
             }
         }
         _context.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(employeeRoleSelections));
 }
        public async Task <IActionResult> Create([Bind("CourseCode,StudentId,Grade")] AcademicRecord academicRecord)
        {
            if (ModelState.IsValid)
            {
                var exists = (from e in _context.AcademicRecord where string.Compare(e.StudentId, academicRecord.StudentId) == 0 && string.Compare(e.CourseCode, academicRecord.CourseCode) == 0 select e).FirstOrDefault <AcademicRecord>();

                if (exists == null)
                {
                    _context.Add(academicRecord);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
                else
                {
                    ModelState.AddModelError("CourseCode", "The student has already had an academic record for this course");
                }
            }
            var courses = (from a in _context.Course select new { Code = a.Code, Title = a.Code + " - " + a.Title }).ToList();

            ViewData["CourseCode"] = new SelectList(courses, "Code", "Title");

            var students = (from b in _context.Student select new { Id = b.Id, Name = b.Id + " - " + b.Name }).ToList();

            ViewData["StudentId"] = new SelectList(students, "Id", "Name");
            return(View(academicRecord));
        }
Beispiel #4
0
        public async Task <IActionResult> Create([Bind("Code,Title,Description,HoursPerWeek,FeeBase")] Course course)
        {
            if (ModelState.IsValid)
            {
                _context.Add(course);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(course));
        }
        public async Task <IActionResult> Create([Bind("Id,Name,UserName,Password")] Employee employee)
        {
            if (ModelState.IsValid)
            {
                _context.Add(employee);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(employee));
        }
        public async Task <IActionResult> Create([Bind("Id,Name")] Student student)
        {
            if (ModelState.IsValid)
            {
                _context.Add(student);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(student));
        }
Beispiel #7
0
        public async Task <IActionResult> Create([Bind("CourseCode,StudentId,Grade")] AcademicRecord academicRecord)
        {
            if (ModelState.IsValid)
            {
                _context.Add(academicRecord);
                await _context.SaveChangesAsync();

                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));
        }
Beispiel #8
0
        public async Task <IActionResult> Create([Bind("CourseCode,StudentId,Grade")] AcademicRecord academicRecord)
        {
            var existing = _context.AcademicRecord.Where(a => a.CourseCode.Equals(academicRecord.CourseCode) && a.StudentId.Equals(academicRecord.StudentId)).FirstOrDefault();

            ViewData["Error"] = "";
            if (existing != null)
            {
                ViewData["Error"] = "The student already has an academic record for the course.";
            }
            else
            {
                if (ModelState.IsValid)
                {
                    _context.Add(academicRecord);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
            }

            List <object> courses = new List <object>();

            foreach (var course in _context.Course)
            {
                courses.Add(new
                {
                    Code  = course.Code,
                    Title = course.Code + " - " + course.Title
                });
            }

            ViewData["CourseCode"] = new SelectList(courses, "Code", "Title");

            List <object> students = new List <object>();

            foreach (var student in _context.Student)
            {
                students.Add(new
                {
                    Id   = student.Id,
                    Name = student.Id + " - " + student.Name
                });
            }

            ViewData["StudentId"] = new SelectList(students, "Id", "Name");
            return(View(academicRecord));
        }
Beispiel #9
0
        public async Task <IActionResult> Create([Bind("CourseCode,StudentId,Grade")] AcademicRecord academicRecord)
        {
            if (ModelState.IsValid)
            {
                if (!AcademicRecordExists(academicRecord.StudentId, academicRecord.CourseCode))
                {
                    _context.Add(academicRecord);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction("Index"));
                }
                else
                {
                    ViewBag.ErrorStyle = "color:red; display:block";
                }
            }

            SetCourseAndStudentViewData();
            return(View(academicRecord));
        }
        public async Task <IActionResult> Create([Bind("CourseCode,StudentId,Grade")] AcademicRecord academicRecord)
        {
            // Check for duplicate records:
            if ((from a in _context.AcademicRecord
                 where a.CourseCode.Equals(academicRecord.CourseCode) &&
                 a.StudentId.Equals(academicRecord.StudentId)
                 select a).FirstOrDefault() != null)
            { // Duplicate record exists
                ViewData["Error"] = "Student already has an academic record for the course.";
            }
            else if (ModelState.IsValid)
            {
                _context.Add(academicRecord);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            // Include course titles and student names in drop down lists
            populateCreateViewData();

            return(View(academicRecord));
        }
Beispiel #11
0
        public async Task <IActionResult> Create([Bind("CourseCode,StudentId,Grade")] AcademicRecord academicRecord)
        {
            var selectedAccount = (from account in _context.AcademicRecord where account.StudentId == academicRecord.StudentId && account.CourseCode == academicRecord.CourseCode select account).FirstOrDefault();

            if (selectedAccount != null)
            {
                ModelState.AddModelError("", "The Student Already Exists Within The System.");
            }
            else
            {
                if (ModelState.IsValid)
                {
                    _context.Add(academicRecord);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
            }

            List <object> courses = new List <object>();

            foreach (Course course in _context.Course)
            {
                courses.Add(new { Code = course.Code, Title = course.Code + "-" + course.Title });
            }

            List <object> students = new List <object>();

            foreach (Student student in _context.Student)
            {
                students.Add(new { Id = student.Id, Name = student.Id + "-" + student.Name });
            }


            ViewData["CourseCode"] = new SelectList(courses, "Code", "Title", academicRecord.CourseCode);
            ViewData["StudentId"]  = new SelectList(students, "Id", "Name", academicRecord.StudentId);
            return(View(academicRecord));
        }
        public async Task <IActionResult> Create([Bind("CourseCode,StudentId,Grade")] AcademicRecord academicRecord)
        {
            if (ModelState.IsValid)
            {
                //query if student ID + courseCode already exists
                var academicExist = (from acExist in _context.AcademicRecord
                                     where string.Compare(acExist.StudentId, academicRecord.StudentId) == 0 &&
                                     string.Compare(acExist.CourseCode, academicRecord.CourseCode) == 0
                                     select acExist).FirstOrDefault <AcademicRecord>();

                //if academic record does not exist yet:
                if (academicExist == null)
                {
                    //adding an academic record
                    _context.Add(academicRecord);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
                else
                {
                    //display error:
                    ModelState.AddModelError("", "The academic record of this course already exists!");
                }
            }

            //display updated view
            var courses = (from c in _context.Course select new { Code = c.Code, Title = c.Code + " - " + c.Title }).ToList();

            ViewData["CourseCode"] = new SelectList(courses, "Code", "Title");

            var students = (from st in _context.Student select new { Id = st.Id, Name = st.Id + " - " + st.Name }).ToList();

            ViewData["StudentId"] = new SelectList(students, "Id", "Name");
            return(View());
        }