public ActionResult Delete(int id) { StudentShared studentShared = repository.getStudentShared((int)id); int theCourseId = studentShared.CourseId; repository.DeleteStudentShared((int)id); // remove the file also string filePath = Path.Combine(Server.MapPath("~/Uploads/StudentsShared/"), studentShared.CourseId.ToString() + "_" + studentShared.StudentId.ToString() + "_" + studentShared.Id.ToString() + "_" + studentShared.FileName); try { if (System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } } catch (Exception e) { ViewBag.Error(e.Message); } return(RedirectToAction("Index", theCourseId)); }
public void DeleteStudentShared(int id) { StudentShared studentShared = db.StudentShareds.Find(id); db.StudentShareds.Remove(studentShared); db.SaveChanges(); // Updates all changed objects }
public ActionResult Edit(int?id) { List <StudentShared> ListStudentShared = repository.GetAllStudentShared(); StudentShared studentShared = ListStudentShared.FirstOrDefault((i => i.Id == id)); return(View(studentShared)); }
public ActionResult Create([Bind(Include = "Id,CourseId,StudentId,Description,FileName")] StudentShared studentShared, HttpPostedFileBase FileName) { try { // TODO: Add insert logic here studentShared.FileName = FileName.FileName; if (ModelState.IsValid) { repository.CreateStudentShared(studentShared); } if (FileName != null && FileName.ContentLength > 0) { string subPath1 = "~/Uploads/StudentsShared/" + studentShared.CourseId + "_" + studentShared.StudentId + "_" + studentShared.Id + "_"; string subPath2 = Path.GetFileName(FileName.FileName); string filePath = Server.MapPath(subPath1 + subPath2); FileName.SaveAs(filePath); } return(RedirectToAction("Index", new { id = Session["CourseID"] })); } catch { return(View()); } }
public ActionResult DeleteConfirmed(int id) { StudentShared studentShared = db.StudentShareds.Find(id); db.StudentShareds.Remove(studentShared); db.SaveChanges(); return(RedirectToAction("Index")); }
public FileResult Download(StudentShared studentShared) { string fileName = "~/Uploads/StudentsShared/" + studentShared.CourseId + "_" + studentShared.StudentId + "_" + studentShared.Id + "_" + studentShared.FileName; string contentType = "application/pdf"; return(new FilePathResult(fileName, contentType) { FileDownloadName = studentShared.FileName }); }
public ActionResult Edit([Bind(Include = "Id,CourseId,StudentId,Description,FileName")] StudentShared studentShared) { if (ModelState.IsValid) { db.Entry(studentShared).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.CourseId = new SelectList(db.Courses, "Id", "Name", studentShared.CourseId); ViewBag.StudentId = new SelectList(db.Students, "Id", "SSN", studentShared.StudentId); return(View(studentShared)); }
public ActionResult Edit([Bind(Include = "Id,CourseId,StudentId,Description,FileName")] StudentShared studentShared, HttpPostedFileBase FileName) { int length = FileName.FileName.Length; int index = FileName.FileName.LastIndexOf('\\') + 1; string path = FileName.FileName.Substring(0, index); string filename = FileName.FileName.Substring(index); //update database studentShared.FileName = filename; repository.UpdateDbStudentShared(studentShared); string oldFiles = studentShared.CourseId + "_" + studentShared.StudentId + "_" + studentShared.Id + "_*.pdf"; try { // remove old file foreach (string DeleteFileName in Directory.EnumerateFiles (Server.MapPath("~/Uploads/StudentsShared/"), oldFiles)) { System.IO.File.Delete(Path.Combine(Server.MapPath("~/Uploads/StudentsShared/"), DeleteFileName)); } //repository.CreateFile() if (FileName != null && FileName.ContentLength > 0) { string filePath = Path.Combine(Server.MapPath("~/Uploads/StudentsShared/"), studentShared.CourseId.ToString() + "_" + studentShared.StudentId.ToString() + "_" + studentShared.Id + "_" + Path.GetFileName(FileName.FileName)); // store new file FileName.SaveAs(filePath); return(RedirectToAction("Index", studentShared.CourseId)); } } catch (Exception e) { ViewBag.Error(e.Message); return(RedirectToAction("Index", studentShared.CourseId)); } return(View()); }
// GET: StudentShareds/Details/5 public ActionResult Details(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } StudentShared studentShared = db.StudentShareds.Find(id); if (studentShared == null) { return(HttpNotFound()); } return(View(studentShared)); }
// GET: StudentShareds/Edit/5 public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } StudentShared studentShared = db.StudentShareds.Find(id); if (studentShared == null) { return(HttpNotFound()); } ViewBag.CourseId = new SelectList(db.Courses, "Id", "Name", studentShared.CourseId); ViewBag.StudentId = new SelectList(db.Students, "Id", "SSN", studentShared.StudentId); return(View(studentShared)); }
public ActionResult Delete(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } int sid = (int)id; StudentShared studentShared = repository.getStudentShared(sid); if (studentShared == null) { return(HttpNotFound()); } return(View(studentShared)); }
public ActionResult Create([Bind(Include = "Id,CourseId,StudentId,Description,FileName")] StudentShared studentShared) { // to include in code //bool exists = System.IO.Directory.Exists(Server.MapPath(subPath)); //if (!exists) // System.IO.Directory.CreateDirectory(Server.MapPath(subPath)); if (ModelState.IsValid) { db.StudentShareds.Add(studentShared); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.CourseId = new SelectList(db.Courses, "Id", "Name", studentShared.CourseId); ViewBag.StudentId = new SelectList(db.Students, "Id", "SSN", studentShared.StudentId); return(View(studentShared)); }
public void UpdateDbStudentShared(StudentShared studentShared) { db.Entry(studentShared).State = System.Data.Entity.EntityState.Modified; // Ej using för då blir det knas på retunr typerna i get metoderna... db.SaveChanges(); // Updates all changed objects }
public void CreateStudentShared(StudentShared studentShared) { db.StudentShareds.Add(studentShared); db.SaveChanges(); // Updates all changed objects }