public IActionResult CreateStudent() { var courses = this.courseService.GetAllWithouPages <CourseDropDownViewModel>(); var viewModel = new CreateStudentInputViewModel { Courses = courses, }; return(this.View(viewModel)); }
public IActionResult CreateStudent(CreateStudentInputViewModel input) { if (!this.ModelState.IsValid) { var courses = this.courseService.GetAllWithouPages <CourseDropDownViewModel>(); input.Courses = courses; return(this.View(input)); } var course = this.courseService.GetById(input.CourseId); Student student = new Student(input.Name, input.MiddleName, input.LastName, input.BirthDate, input.Gender, input.Phone, input.Address, course); List <Subject> subjectsToAdd = this.subjectService.GetAllSubjectsInCourse(course.Id); student.Subjects = this.studentsService.FillStudentSubjectsCollection(subjectsToAdd, student.Id); student.SubjectsNames = this.subjectService.GetAllSubjectNameInCourses(student.Subjects); string uniqueFileName = string.Empty; if (input.Photo != null) { string uploadsFolder = Path.Combine(this.hostingEnvironment.WebRootPath, "css\\StudentImages"); uniqueFileName = Guid.NewGuid().ToString() + "_" + input.Photo.FileName; string filePath = Path.Combine(uploadsFolder, uniqueFileName); input.Photo.CopyTo(new FileStream(filePath, FileMode.Create)); student.PhotoPath = uniqueFileName; } using (this.db) { this.db.Students.Add(student); this.db.SaveChanges(); } return(this.RedirectToAction("ByName", "Course", new { course.Name })); }