public ActionResult CreateComponent(string componentType, int enrollmentId, string enrollmentType) { var component = new Component { CreatorId = Convert.ToInt32(User.Identity.GetUserId()), Type = (ComponentType)Enum.Parse(typeof(ComponentType), componentType), EnrollmentId = enrollmentId }; db.Components.Add(component); db.SaveChanges(); var enrollment = new Enrollment { DateStart = DateTime.Now, EnrolledId = enrollmentId, EnrolledType = (ComponentType)Enum.Parse(typeof(ComponentType), enrollmentType), EnrolleeId = component.ComponentId, EnrolleeType = (ComponentType)Enum.Parse(typeof(ComponentType), componentType), RoleId = (int)Role.Teacher }; db.Enrollments.Add(enrollment); db.SaveChanges(); string viewName = ""; switch ((ComponentType)Enum.Parse(typeof(ComponentType), componentType)) { case ComponentType.Course: viewName = "CreateorUpdateCourse"; break; case ComponentType.Topic: viewName = "CreateTopic"; break; case ComponentType.Module: viewName = "CreateModule"; break; } return PartialView(viewName, component); }
public ActionResult CreateOrUpdateCourse(int? courseId) { var model = new CreateCourseViewModel(); Component course; var userId = User.Identity.GetUserId<int>(); if (!courseId.HasValue) { course = new Component { CreatorId = Convert.ToInt32(User.Identity.GetUserId()), Created = DateTime.Now, Type = ComponentType.Course }; db.Components.Add(course); var enrollment = new Enrollment { DateStart = DateTime.Now, EnrolledId = userId, EnrolledType = ComponentType.User, EnrolleeId = course.ComponentId, EnrolleeType = ComponentType.Course, RoleId = (int)Role.Teacher }; db.Enrollments.Add(enrollment); db.SaveChanges(); model.Course = course; model.Topics = new List<Component>(); } else { course = (from a in db.Components where a.ComponentId == courseId select a).FirstOrDefault(); var topics = from a in db.Enrollments where a.EnrolledId == courseId && a.EnrolleeType == ComponentType.Topic join b in db.Components on a.EnrolleeId equals b.ComponentId select b; course.ImagePath = System.IO.File.Exists(Server.MapPath("/Resources/" + course.ComponentId + ".jpg")) ? ("../Resources/" + course.ComponentId + ".jpg") : "../Resources/NoImage.jpg"; foreach (var topic in topics) { topic.ImagePath = System.IO.File.Exists("../Resources/" + course.ComponentId + ".jpg") ? ("../Resources/" + course.ComponentId + ".jpg") : "../Resources/NoImage.jpg"; topic.EnrollmentId = course.ComponentId; } model.Course = course; model.Topics = topics; } return View(model); }