public ActionResult Create(CreateInstructorViewModel model, string[] selectedCourses) { if (selectedCourses != null) { model.Courses = new List <Course>(); foreach (var course in selectedCourses) { var courseToAdd = db.Courses.Find(int.Parse(course)); model.Courses.Add(courseToAdd); } } if (ModelState.IsValid) { Instructor instructor = new Instructor(); instructor.LastName = model.LastName; instructor.FirstMidName = model.FirstMidName; instructor.HireDate = model.HireDate; instructor.OfficeAssignment = model.OfficeAssignment; instructor.Courses = model.Courses; db.Instructors.Add(instructor); db.SaveChanges(); return(RedirectToAction("Index")); } PopulateViewBagCourses(model); return(View(model)); }
public IActionResult CreatePost(CreateInstructorViewModel model) { if (ModelState.IsValid) { _instructorRepository.Add(model.Instructor); var instructorId = model.Instructor.InstructorId; var courseAssignment = new List <CourseAssignment>(); if (model.AssignedCourseData != null) { foreach (var data in model.AssignedCourseData) { if (data.Assigned) { //courseAssignment.Add(new CourseAssignment(){CourseId = data.CourseId,InstructorId = instructorId}); _courseAssignmentRepository.Add(new CourseAssignment() { CourseId = data.CourseId, InstructorId = instructorId }); } } } return(RedirectToAction("Index")); } return(View("Create")); }
// GET: Instructors/Create public ActionResult Create() { // Create a new instance of a CreateInstructorViewModel // If we want to get all the cohorts, we need to use the constructor that's expecting a connection string. // When we create this instance, the constructor will run and get all the cohorts. CreateInstructorViewModel instructorViewModel = new CreateInstructorViewModel(_config.GetConnectionString("DefaultConnection")); // Once we've created it, we can pass it to the view return(View(instructorViewModel)); }
public IActionResult EditPost(CreateInstructorViewModel model) { if (ModelState.IsValid) { _instructorRepository.Update(model.Instructor); var instructorId = model.Instructor.InstructorId; if (model.AssignedCourseData != null) { foreach (var data in model.AssignedCourseData) { { if (data.Assigned) { var isExist = IsExist(_courseAssignmentRepository.GetAll(), instructorId, data.CourseId); if (!isExist) { _courseAssignmentRepository.Add(new CourseAssignment() { CourseId = data.CourseId, InstructorId = instructorId }); } } else { var isExist = IsExist(_courseAssignmentRepository.GetAll(), instructorId, data.CourseId); if (isExist) { var filter = _courseAssignmentRepository .GetByFiler(x => x.CourseId == data.CourseId && x.InstructorId == instructorId) .FirstOrDefault(); _courseAssignmentRepository.Delete(filter); } } } return(RedirectToAction("Index")); } } return(View("Create")); } return(View("Edit")); }
private void PopulateViewBagCourses(CreateInstructorViewModel model = null) { var allCourses = db.Courses; var instructorCourses = model == null ? null : new HashSet <int>(model.Courses.Select(c => c.CourseID)); var viewModel = new List <AssignedCourseData>(); foreach (var course in allCourses) { viewModel.Add(new AssignedCourseData { CourseID = course.CourseID, Title = course.Title, Assigned = instructorCourses == null ? false : instructorCourses.Contains(course.CourseID) }); } ViewBag.Courses = viewModel; }
public async Task <IActionResult> Create(CreateInstructorViewModel viewmodel) { string sql = $@"INSERT INTO Instructor ( FirstName, LastName, SlackHandle, CohortId, Specialty ) VALUES ( '{viewmodel.instructor.FirstName}', '{viewmodel.instructor.LastName}', '{viewmodel.instructor.SlackHandle}', '{viewmodel.instructor.CohortId}', '{viewmodel.instructor.Specialty}' );"; using (IDbConnection conn = Connection) { var newId = await conn.ExecuteAsync(sql); return(RedirectToAction(nameof(Index))); } }
public async Task <IActionResult> Edit(int id) { var instructor = _instructorRepository.GetById(id); var allCourses = _courseRepository.GetAll(); var coursesToInstructor = await _courseAssignmentRepository.CoursesToInstructorAsync(instructor.InstructorId); var model = new CreateInstructorViewModel() { Instructor = instructor, AssignedCourseData = allCourses.Select(s => new AssignedCourseData() { CourseId = s.CourseId, CourseName = s.CourseName, Assigned = coursesToInstructor.Exists(x => x.Course.CourseId == s.CourseId) }).OrderBy(x => x.CourseName).ToList() }; return(View(model)); }
public async Task <ActionResult> Create(CreateInstructorViewModel model) { using (SqlConnection conn = Connection) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = @"INSERT INTO Instructor ( FirstName, LastName, SlackHandle, CohortId ) VALUES ( @firstName, @lastName, @slackHandle, @cohortId )"; cmd.Parameters.Add(new SqlParameter("@firstName", model.instructor.FirstName)); cmd.Parameters.Add(new SqlParameter("@lastName", model.instructor.LastName)); cmd.Parameters.Add(new SqlParameter("@slackHandle", model.instructor.SlackHandle)); cmd.Parameters.Add(new SqlParameter("@cohortId", model.instructor.CohortId)); cmd.ExecuteNonQuery(); return(RedirectToAction(nameof(Index))); } } }
public async Task <ActionResult> NewStaffMember(CreateInstructorViewModel model) { if (!ModelState.IsValid) { return(View(model)); } var user = new Instructor { UserName = model.Username, Email = model.Email, AddressLine1 = model.AddressLine1, AddressLine2 = model.AddressLine2, City = model.City, Postcode = model.Postcode, First_Name = model.Firstname, Last_Name = model.Lastname, PhoneNumber = model.PhoneNumber, Date_Of_Birth = model.Date_Of_Birth, Bio = model.Bio, Experience_Years = model.Experience_Years }; var result = await UserManager.CreateAsync(user, model.Password); //find the instructor and assign him to the instructor role Instructor instructor = (Instructor)await UserManager.FindByEmailAsync(model.Email); var AddingToRole = await UserManager.AddToRoleAsync(instructor.Id, "Instructor"); //if both have succeeded everything is successful return the admin to the control panel if (result.Succeeded && AddingToRole.Succeeded) { return(RedirectToAction("ControlPanel")); } //if we got here something went wrong return(View(model)); }
// GET: Instructors/Create public ActionResult Create() { var model = new CreateInstructorViewModel(_config); return(View(model)); }