public ActionResult Create(int?CourseId, int?ModuleId, int?ActivityId) { if (CourseId == null && ModuleId == null && ActivityId == null) { return(RedirectToAction("Index", "Courses")); } var model = new DocumentInsertViewModel { Types = DocumentTypeSelectList(), }; if (ActivityId != null) { Activity activity = db.Activities.Find(ActivityId); model.ActivityId = activity?.Id; model.ActivityName = activity?.Name; } if (ModuleId != null) { Module module = db.Modules.Find(ModuleId); model.ModuleId = module?.Id; model.ModuleName = module?.Name; } if (CourseId != null) { Course course = db.Courses.Find(CourseId); model.CourseId = course?.Id; model.CourseName = course?.Name; } return(View(model)); }
public ActionResult Create([Bind(Include = "SelectedTypeId,File,Description,Deadline,CourseId,ModuleId,ActivityId")] DocumentInsertViewModel model) { if (ModelState.IsValid) { string path = Server.MapPath("~/Documents"); string relativePath = ""; string filename = Path.GetFileName(model.File.FileName); if (model.CourseId != null) { relativePath = Path.Combine("course", model.CourseId.ToString()); } else if (model.ModuleId != null) { relativePath = Path.Combine("module", model.ModuleId.ToString()); } else if (model.ActivityId != null) { relativePath = Path.Combine("activity", model.ActivityId.ToString()); } path = Path.Combine(path, relativePath); Directory.CreateDirectory(path); string fullPath = Path.Combine(path, filename); int nr = 1; while (System.IO.File.Exists(fullPath)) { filename = Path.GetFileNameWithoutExtension(filename) + "_" + nr + Path.GetExtension(filename); fullPath = Path.Combine(path, filename); nr++; } model.File.SaveAs(fullPath); var document = new Document { TypeId = model.SelectedTypeId, UserId = User.Identity.GetUserId(), Description = model.Description, Deadline = model.Deadline, RelativePath = relativePath, FileName = filename, MimeType = model.File.ContentType, FullPath = fullPath, CourseId = model.CourseId, ModuleId = model.ModuleId, ActivityId = model.ActivityId }; db.Documents.Add(document); db.SaveChanges(); return(RedirectToParent(document)); } return(View(model)); }