コード例 #1
0
        public async Task <IActionResult> Create([Bind("SubjectID,Credits,DepartmentID,Title")] Subject subject)
        {
            if (ModelState.IsValid)
            {
                _context.Add(subject);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            PopulateDepartmentsDropDownList(subject.DepartmentID);
            return(View(subject));
        }
コード例 #2
0
        public async Task <IActionResult> Create([Bind("DepartmentID,Name,TeacherID")] Department department)
        {
            if (ModelState.IsValid)
            {
                _context.Add(department);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["TeacherID"] = new SelectList(_context.Teachers, "TeacherID", "FirstName", department.TeacherID);
            return(View(department));
        }
コード例 #3
0
        public async Task <IActionResult> Create([Bind("EnrollmentDate,FirstName,LastName")] Student student)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    _context.Add(student);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
            }
            catch (DbUpdateException /* ex */)
            {
                //Log the error (uncomment ex variable name and write a log.
                ModelState.AddModelError("", "Unable to save changes. " +
                                         "Try again, and if the problem persists " +
                                         "see your system administrator.");
            }
            return(View(student));
        }
コード例 #4
0
        public async Task <IActionResult> Create([Bind("FirstName,HireDate,LastName,RoomAssignment")] Teacher teacher, string[] selectedSubjects)
        {
            if (selectedSubjects != null)
            {
                teacher.SubjectAssignments = new List <SubjectAssignment>();
                foreach (var subject in selectedSubjects)
                {
                    var subjectToAdd = new SubjectAssignment {
                        TeacherID = teacher.TeacherID, SubjectID = int.Parse(subject)
                    };
                    teacher.SubjectAssignments.Add(subjectToAdd);
                }
            }
            if (ModelState.IsValid)
            {
                _context.Add(teacher);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            PopulateRelatedSubject(teacher);
            return(View(teacher));
        }