public async Task<ActionResult> CreateCourse([Bind(Include = "Id,Title,MaxEnrollments,CourseTemplateId,Note,OverView,Price,IsLive")] CreateCohortViewModel form) { if (ModelState.IsValid) { var courseTemplate = await _context.CourseTemplates.FindAsync(form.CourseTemplateId); var course = new Course { Title = courseTemplate.Title, OverView = courseTemplate.OverView, Note = courseTemplate.Note, MaxEnrollments = form.MaxEnrollments, CourseTemplateId = form.CourseTemplateId, Price = form.Price, IsLive = form.IsLive, }; _context.Courses.Add(course); try { await _context.SaveChangesAsync(); } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx) { Exception raise = dbEx; foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { string errorMessage = string.Format("{0}:{1}", validationErrors.Entry.Entity.ToString(), validationError.ErrorMessage); // raise a new exception nesting // the current instance as InnerException raise = new InvalidOperationException(errorMessage, raise); } } throw raise; } var newCourseId = course.Id; var newCourseTemplateId = course.CourseTemplateId; var courseTemplateResources = _context.CourseTemplateResources.Where(r => r.CourseTemplateId == newCourseTemplateId); //Add the resources associated with the template foreach (var resource in courseTemplateResources) { var courseResource = new CourseResource { CourseId = newCourseId, ResourceId = resource.ResourceId }; _context.CourseResources.Add(courseResource); } await _context.SaveChangesAsync(); return RedirectToAction("Index"); } ViewBag.Courseid = new SelectList(_context.Products, "Id", "Title"); return View(form); }
public ActionResult AssociateResourceWithCohort(CourseResourceViewModel model) { var cohortId = Convert.ToInt32(model.CourseId); var resourceId = Convert.ToInt32(model.ResourceId); var courseResource = new CourseResource { CourseId = cohortId, ResourceId = resourceId }; _context.CourseResources.Add(courseResource); _context.SaveChanges(); return RedirectToAction("CourseResources", "ManageCourses", new { id = cohortId }); }
public async Task<ActionResult> CreateResource(CourseResourceViewModel form, HttpPostedFileBase file) { if (ModelState.IsValid) { if (file != null) { string fileName = Path.GetFileName(file.FileName); if (fileName != null) { var destinationPath = Path.Combine(Server.MapPath("~/Content/Resources"), fileName); file.SaveAs(destinationPath); } form.Resource.UploadedFileName = fileName; } var courseId = form.CourseId; _context.Resources.Add(form.Resource); await _context.SaveChangesAsync(); var courseResource = new CourseResource { CourseId = courseId, ResourceId = form.Resource.Id }; _context.CourseResources.Add(courseResource); await _context.SaveChangesAsync(); //return RedirectToAction("CourseResources", "ManageCourses", new { id = cohortId }); return RedirectToAction("Board", "Courses", new { area="", id = courseId }); } // ViewBag.ResourceTypeId = new SelectList(_context.ResourceTypes, "Id", "ParentId", form.Resource.ResourceTypeId); return View(form); }
public async Task<ActionResult> CreateCourse([Bind(Include = "Id,Title,MaxEnrollments,CourseTemplateId,Note,OverView,Price,IsLive")] CreateCohortViewModel form) { if (ModelState.IsValid) { var courseTemplate = await _context.CourseTemplates.FindAsync(form.CourseTemplateId); var course = new Course { Title = courseTemplate.Title, MaxEnrollments = form.MaxEnrollments, CourseTemplateId = form.CourseTemplateId, OverView = form.OverView, Price = form.Price, IsLive = form.IsLive, }; _context.Courses.Add(course); await _context.SaveChangesAsync(); var newCourseId = course.Id; var newCourseTemplateId = course.CourseTemplateId; var courseTemplateResources = _context.CourseTemplateResources.Where(r => r.CourseId == newCourseTemplateId); //Add the resources associated with the template foreach (var resource in courseTemplateResources) { var courseResource = new CourseResource { CourseId = newCourseId, ResourceId = resource.ResourceId }; _context.CourseResources.Add(courseResource); } await _context.SaveChangesAsync(); return RedirectToAction("Index"); } ViewBag.Courseid = new SelectList(_context.Products, "Id", "Title"); return View(form); }