public ActionResult deleteNstatusCommand_chapter(IEnumerable <int> chid, string command) { foreach (int chapterid in chid) { ref_chapters chapter = db.ref_chapters.Where(a => a.ChapterId == chapterid).SingleOrDefault(); if (command == "Delete") { db.ref_chapters.Remove(chapter); if (System.IO.File.Exists((chapter.ContentPath))) { System.IO.File.Delete((chapter.ContentPath)); } } db.SaveChanges(); } return(RedirectToAction("Index")); }
public ActionResult Chapter(Chapters obj) { if (ModelState.IsValid) { if (db.ref_chapters.Count(a => a.ChapterName == obj.ChapterName) == 0) { ref_chapters chpt = new ref_chapters(); //int? chid=db.ref_chapters.Max(a => a.ChapterId); //chid = chid == null ? 0 : chid; //chid = chid + 1; //chpt.ChapterId = (int)chid; chpt.CourseId = obj.CourseId; chpt.ChapterName = obj.ChapterName; chpt.Content_Type = obj.Content_Type; var file = obj.ChapContent; var fileName = Path.GetFileName(obj.ChapContent.FileName); var path = Path.Combine(Server.MapPath("~/ChapterContent/"), fileName); string newPath = ""; switch (obj.Content_Type) { case "Word Document": path = Path.Combine(Server.MapPath("~/ChapterContent/Word/"), fileName); newPath = "ChapterContent/Word/" + fileName; break; case "Pdf File": path = Path.Combine(Server.MapPath("~/ChapterContent/Pdf/"), fileName); newPath = "ChapterContent/Pdf/" + fileName; break; case "Presentaion": path = Path.Combine(Server.MapPath("~/ChapterContent/Ppt/"), fileName); newPath = "ChapterContent/Ppt/" + fileName; break; case "Video File": path = Path.Combine(Server.MapPath("~/ChapterContent/Video/"), fileName); newPath = "ChapterContent/Video/" + fileName; break; } file.SaveAs(path); chpt.ContentPath = newPath; //Stream fs = obj.ChapContent.InputStream; //BinaryReader br = new BinaryReader(fs); //Byte[] bytes = br.ReadBytes((Int32)fs.Length); //chpt.Content = bytes; chpt.Sort_Order = obj.Sort_Order; db.ref_chapters.Add(chpt); db.SaveChanges(); } else { TempData["ErrMsg"] = obj.ChapterName + " Chapter already exists"; return(RedirectToAction("Index")); } } return(RedirectToAction("Index")); }