public async Task DeleteAsyncWhenInvalidIdReturnsStudyMaterialNotFoundResponse() { // Arrange var mockUnitOfWork = GetDefaultUnitOfWorkRepositoryInstance(); var mockStudyMaterialRepository = GetDefaultStudyMaterialRepositoryInstance(); var StudyMaterialId = 1; var studyMaterial = new StudyMaterial() { Id = 1, Title = "string", Description = "string", TopicId = 1 }; mockStudyMaterialRepository.Setup(r => r.FindById(StudyMaterialId)).Returns(Task.FromResult <StudyMaterial>(null)); mockStudyMaterialRepository.Setup(r => r.Remove(studyMaterial)); var service = new StudyMaterialService(mockStudyMaterialRepository.Object, mockUnitOfWork.Object); // Act StudyMaterialResponse result = await service.DeleteAsync(StudyMaterialId); var message = result.Message; // Assert message.Should().Be("Study material not found"); }
public async Task UpdateAsyncWhenIdIsCorrectReturnsStudyMaterialInstance() { // Arrange var mockUnitOfWork = GetDefaultUnitOfWorkRepositoryInstance(); var mockStudyMaterialRepository = GetDefaultStudyMaterialRepositoryInstance(); var StudyMaterialId = 1; var studyMaterial = new StudyMaterial() { Id = 1, Title = "string", Description = "string", TopicId = 1 }; mockStudyMaterialRepository.Setup(r => r.FindById(StudyMaterialId)).Returns(Task.FromResult(studyMaterial)); mockStudyMaterialRepository.Setup(r => r.Update(studyMaterial)); var service = new StudyMaterialService(mockStudyMaterialRepository.Object, mockUnitOfWork.Object); // Act StudyMaterialResponse result = await service.UpdateAsync(StudyMaterialId, studyMaterial); var resource = result.Resource; // Assert resource.Should().Equals(studyMaterial); }
public async Task <StudyMaterialResponse> UpdateAsync(int id, StudyMaterial studyMaterial) { var existingStudyMaterial = await _studyMaterialRepository.FindById(id); if (existingStudyMaterial == null) { return(new StudyMaterialResponse("Study material not found")); } existingStudyMaterial.Title = studyMaterial.Title; existingStudyMaterial.Description = studyMaterial.Description; existingStudyMaterial.FileName = studyMaterial.FileName; existingStudyMaterial.FilePath = studyMaterial.FilePath; existingStudyMaterial.Size = studyMaterial.Size; try { _studyMaterialRepository.Update(existingStudyMaterial); await _unitOfWork.CompleteAsync(); return(new StudyMaterialResponse(existingStudyMaterial)); } catch (Exception e) { return(new StudyMaterialResponse("Has ocurred an error updating the study material" + e.Message)); } }
public ActionResult DeleteConfirmed(int id) { StudyMaterial studyMaterial = db.StudyMaterials.Find(id); db.StudyMaterials.Remove(studyMaterial); db.SaveChanges(); return(RedirectToAction("Index")); }
public void ShowStudyMaterial(StudyMaterial whichone, bool showOrHide = true) { StudyMaterialType myStudyMaterialtype = this.GetComponentInChildren <StudyMaterialType>(); if (myStudyMaterialtype != null) { myStudyMaterialtype.SetStudyMaterialVisiblity(whichone, showOrHide); } }
public void EnableMyItem(StudyMaterial whichOne, bool val) { if (myStudyMaterials != null) { if (deskOccupied) { myStudyMaterials.gameObject.SetActive(val); myStudyMaterials.SetStudyMaterialVisiblity(whichOne, val); } } }
public ActionResult Edit([Bind(Include = "Id,Title,PublishDate,Content,Description,Source,SectionId,AuthorId")] StudyMaterial studyMaterial) { if (ModelState.IsValid) { db.Entry(studyMaterial).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.AuthorId = new SelectList(db.Users, "Id", "FullName", studyMaterial.AuthorId); ViewBag.SectionId = new SelectList(db.Sections, "Id", "Title", studyMaterial.SectionId); return(View(studyMaterial)); }
// GET: Administration/StudyMaterials/Details/5 public ActionResult Details(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } StudyMaterial studyMaterial = db.StudyMaterials.Find(id); if (studyMaterial == null) { return(HttpNotFound()); } return(View(studyMaterial)); }
public async Task <ActionResult> DeleteConfirmed(int id) { StudyMaterial studyMaterial = await _studyMaterial.GetByIdAsync(Convert.ToInt32(id)); var path = Server.MapPath(studyMaterial.FilePath); if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); } await _studyMaterial.DeleteAsync(studyMaterial); _studyMaterial.UnitOfWorkSaveChanges(); return(RedirectToAction("Index")); }
// GET: Administration/StudyMaterials/Edit/5 public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } StudyMaterial studyMaterial = db.StudyMaterials.Find(id); if (studyMaterial == null) { return(HttpNotFound()); } ViewBag.AuthorId = new SelectList(db.Users, "Id", "FullName", studyMaterial.AuthorId); ViewBag.SectionId = new SelectList(db.Sections, "Id", "Title", studyMaterial.SectionId); return(View(studyMaterial)); }
// GET: Admin/StudyMaterialsResources/Details/5 public async Task <ActionResult> Details(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } StudyMaterial studyMaterials = await _studyMaterial.GetByIdAsync(Convert.ToInt32(id)); if (studyMaterials == null) { return(HttpNotFound()); } var studyMaterialsResources = Mapper.Map <StudyMaterial, StudyMaterialsResources>(studyMaterials); return(View(studyMaterialsResources)); }
// GET: Admin/StudyMaterialsResources/Edit/5 public async Task <ActionResult> Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } StudyMaterial studyMaterial = await _studyMaterial.GetByIdAsync(Convert.ToInt32(id)); if (studyMaterial == null) { return(HttpNotFound()); } var studyMaterialsResources = Mapper.Map <StudyMaterial, StudyMaterialsResources>(studyMaterial); var Subcategorylist = _subCategoryService.GetAllAsync(); ViewBag.SubcategoryID = new SelectList(Subcategorylist.Result.Where(i => i.Status == true), "Id", "Name"); return(View(studyMaterialsResources)); }
public async Task <ResultMessage <bool> > Move(int studyMaterialId, int categoryId) { try { StudyMaterial studyMaterial = await _context.StudyMaterials.SingleOrDefaultAsync(sm => sm.StudyMaterialId == studyMaterialId); if (studyMaterial == null) { return(new ResultMessage <bool>(false, OperationStatus.NotFound)); } studyMaterial.CategoryId = categoryId; await _context.SaveChangesAsync(); return(new ResultMessage <bool>(true, OperationStatus.Success)); } catch (DbUpdateException ex) { OperationStatus status = _errorHandler.Handle(ex); return(new ResultMessage <bool>(false, status)); } }
public async Task <StudyMaterialResponse> SaveAsync(int topicId, StudyMaterial studyMaterial) { var existingTopic = await _topicRepository.FindById(topicId); if (existingTopic == null) { return(new StudyMaterialResponse("Topic Id not found")); } try { studyMaterial.TopicId = topicId; await _studyMaterialRepository.AddAsync(studyMaterial); await _unitOfWork.CompleteAsync(); return(new StudyMaterialResponse(studyMaterial)); } catch (Exception e) { return(new StudyMaterialResponse("Has ocurred an error saving the study material" + e.Message)); } }
public bool SetStudyMaterialVisiblityAndRetrun(StudyMaterial studyMaterial, bool visibleOrNot) { switch (studyMaterial) { case StudyMaterial.BoxFilled: if (BoxFilled != null) { BoxFilled.SetActive(visibleOrNot); } else { return(false); } break; case StudyMaterial.YellowPaper: if (YellowPaper != null) { YellowPaper.SetActive(visibleOrNot); } else { return(false); } break; case StudyMaterial.BluePaper: if (BluePaper != null) { BluePaper.SetActive(visibleOrNot); } else { return(false); } break; case StudyMaterial.WorkSheet: if (WorkSheet != null) { WorkSheet.SetActive(visibleOrNot); } else { return(false); } break; case StudyMaterial.Book: if (Book != null) { Book.SetActive(visibleOrNot); } else { return(false); } break; case StudyMaterial.Tablet: if (Tablet != null) { Tablet.SetActive(visibleOrNot); } else { return(false); } break; case StudyMaterial.NoteBook: if (NoteBook != null) { NoteBook.SetActive(visibleOrNot); } else { return(false); } break; case StudyMaterial.OpenBook: if (OpenBook != null) { OpenBook.SetActive(visibleOrNot); } else { return(false); } break; case StudyMaterial.PencilBox: if (PencilBox != null) { PencilBox.SetActive(visibleOrNot); } else { return(false); } break; case StudyMaterial.Pencil: if (Pencil != null) { Pencil.SetActive(visibleOrNot); } else { return(false); } break; case StudyMaterial.Pen: if (Pen != null) { Pen.SetActive(visibleOrNot); } else { return(false); } break; case StudyMaterial.LunchBox: if (LunchBox != null) { LunchBox.SetActive(visibleOrNot); } else { return(false); } break; case StudyMaterial.MilkBottle: if (MilkBottle != null) { MilkBottle.SetActive(visibleOrNot); } else { return(false); } break; case StudyMaterial.Bag: if (Bag != null) { Bag.SetActive(visibleOrNot); } else { return(false); } break; case StudyMaterial.Ruler: if (Ruler != null) { Ruler.SetActive(visibleOrNot); } else { return(false); } break; case StudyMaterial.Eraser: if (Eraser != null) { Eraser.SetActive(visibleOrNot); } else { return(false); } break; case StudyMaterial.Glue: if (Glue != null) { Glue.SetActive(visibleOrNot); } else { return(false); } break; case StudyMaterial.Scissor: if (Scissor != null) { Scissor.SetActive(visibleOrNot); } else { return(false); } break; case StudyMaterial.RippedPaper: if (RippedPaper != null) { RippedPaper.SetActive(visibleOrNot); } else { return(false); } break; case StudyMaterial.Apple: if (Apple != null) { Apple.SetActive(visibleOrNot); } else { return(false); } break; default: if (BluePaper != null) { BluePaper.SetActive(visibleOrNot); } else { return(false); } break; } return(true); }
public void SetStudyMaterialVisiblity(StudyMaterial studyMaterial, bool visibleOrNot) { switch (studyMaterial) { case StudyMaterial.BoxFilled: BoxFilled.SetActive(visibleOrNot); break; case StudyMaterial.YellowPaper: YellowPaper.SetActive(visibleOrNot); break; case StudyMaterial.BluePaper: BluePaper.SetActive(visibleOrNot); break; case StudyMaterial.WorkSheet: WorkSheet.SetActive(visibleOrNot); break; case StudyMaterial.Book: Book.SetActive(visibleOrNot); break; case StudyMaterial.Tablet: Tablet.SetActive(visibleOrNot); break; case StudyMaterial.NoteBook: NoteBook.SetActive(visibleOrNot); break; case StudyMaterial.OpenBook: OpenBook.SetActive(visibleOrNot); break; case StudyMaterial.PencilBox: PencilBox.SetActive(visibleOrNot); break; case StudyMaterial.Pencil: Pencil.SetActive(visibleOrNot); break; case StudyMaterial.Pen: Pen.SetActive(visibleOrNot); break; case StudyMaterial.LunchBox: LunchBox.SetActive(visibleOrNot); break; case StudyMaterial.MilkBottle: MilkBottle.SetActive(visibleOrNot); break; case StudyMaterial.Bag: Bag.SetActive(visibleOrNot); break; case StudyMaterial.Ruler: Ruler.SetActive(visibleOrNot); break; case StudyMaterial.Eraser: Eraser.SetActive(visibleOrNot); break; case StudyMaterial.Glue: Glue.SetActive(visibleOrNot); break; case StudyMaterial.Scissor: Scissor.SetActive(visibleOrNot); break; case StudyMaterial.RippedPaper: RippedPaper.SetActive(visibleOrNot); break; case StudyMaterial.Apple: Apple.SetActive(visibleOrNot); break; case StudyMaterial.WeeklyPlan: WeeklyPlan.SetActive(visibleOrNot); break; default: BluePaper.SetActive(visibleOrNot); break; } }
public GameObject GetStudyMaterial(StudyMaterial studyMaterial) { switch (studyMaterial) { case StudyMaterial.BoxFilled: return(BoxFilled); case StudyMaterial.YellowPaper: return(YellowPaper); case StudyMaterial.BluePaper: return(BluePaper); case StudyMaterial.WorkSheet: return(WorkSheet); case StudyMaterial.Book: return(Book); case StudyMaterial.Tablet: return(Tablet); case StudyMaterial.NoteBook: return(NoteBook); case StudyMaterial.OpenBook: return(OpenBook); case StudyMaterial.PencilBox: return(PencilBox); case StudyMaterial.Pencil: return(Pencil); case StudyMaterial.Pen: return(Pen); case StudyMaterial.LunchBox: return(LunchBox); case StudyMaterial.MilkBottle: return(MilkBottle); case StudyMaterial.Bag: return(Bag); case StudyMaterial.Ruler: return(Ruler); case StudyMaterial.Eraser: return(Eraser); case StudyMaterial.Glue: return(Glue); case StudyMaterial.Scissor: return(Scissor); case StudyMaterial.RippedPaper: return(RippedPaper); case StudyMaterial.Apple: return(Apple); case StudyMaterial.WeeklyPlan: return(WeeklyPlan); default: return(BluePaper); } }
public void Update(StudyMaterial studyMaterial) { _context.StudyMaterials.Update(studyMaterial); }
public async Task <StudyMaterial> SaveAsync(StudyMaterial studyMaterial) { var result = await _context.StudyMaterials.AddAsync(studyMaterial); return(result.Entity); }
public void Remove(StudyMaterial studyMaterial) { _context.StudyMaterials.Remove(studyMaterial); }
public async Task AddAsync(StudyMaterial studyMaterial) { await _context.StudyMaterials.AddAsync(studyMaterial); }
public ActionResult UploadScanfiles(string str, string FileTypeName, StudyMaterial model) { string userID = User.Identity.Name; var userid = (from a in dbcontext.CommitteeMembers_Profile where a.EmailID == userID select a.CmtID).FirstOrDefault(); string msg = ""; int Status = 0; string fileName = ""; string filesnames = ""; var Stringimages = new List<string>(); string savedFileName = ""; DateTime d1 = DateTime.Now; var storedData = dbcontext.StudyMaterials.Where(up => up.CmtID == userid).ToList(); if (storedData.Count > 0) { if (!System.IO.Directory.Exists(Server.MapPath("\\Content\\Doc\\" + userid))) { System.IO.Directory.CreateDirectory(Server.MapPath("\\Content\\Doc\\" + userid)); for (int i = 0; i < Request.Files.Count; i++) { HttpPostedFileBase file = Request.Files[i]; //Uploaded file //Use the following properties to get file's name, size and MIMEType int fileSize = file.ContentLength; fileName = file.FileName; string mimeType = file.ContentType; System.IO.Stream fileContent = file.InputStream; savedFileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + Path.GetFileName(fileName); file.SaveAs(Path.Combine(Server.MapPath("~/Content/Doc/" + userid), savedFileName)); // Save the file // errormsg.Text += String.Format("{0}<br />", file); Stringimages.Add(savedFileName); } filesnames = string.Join("|||", Stringimages.ToArray()); } else { for (int i = 0; i < Request.Files.Count; i++) { HttpPostedFileBase file = Request.Files[i]; //Uploaded file //Use the following properties to get file's name, size and MIMEType int fileSize = file.ContentLength; fileName = file.FileName; string mimeType = file.ContentType; System.IO.Stream fileContent = file.InputStream; savedFileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + Path.GetFileName(fileName); file.SaveAs(Path.Combine(Server.MapPath("~/Content/Doc/" + userid), savedFileName)); // Save the file // errormsg.Text += String.Format("{0}<br />", file); Stringimages.Add(savedFileName); } filesnames = string.Join("|||", Stringimages.ToArray()); } //data opration perperom here int len = storedData.Count; var smdat = dbcontext.StudyMaterials.FirstOrDefault(s => s.CmtID == userid); { smdat.CourseID = model.CourseID; smdat.SemID = model.SemID; smdat.SM_Description = model.SM_Description; smdat.SM_Name = model.SM_Name; smdat.Attachments = savedFileName; smdat.ModifiedDate = DateTime.Now; } var In = dbcontext.SaveChanges(); ; if (In > 0) { msg = "file uploaded Successfully"; } else { msg = "Error Occured"; } } else { if (!System.IO.Directory.Exists(Server.MapPath("\\Content\\Doc\\" + userid))) { System.IO.Directory.CreateDirectory(Server.MapPath("\\Content\\Doc\\" + userid)); for (int i = 0; i < Request.Files.Count; i++) { HttpPostedFileBase file = Request.Files[i]; //Uploaded file //Use the following properties to get file's name, size and MIMEType int fileSize = file.ContentLength; fileName = file.FileName; string mimeType = file.ContentType; System.IO.Stream fileContent = file.InputStream; savedFileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + Path.GetFileName(fileName); file.SaveAs(Path.Combine(Server.MapPath("~/Content/Doc/" + userid), savedFileName)); // Save the file // errormsg.Text += String.Format("{0}<br />", file); Stringimages.Add(savedFileName); } filesnames = string.Join("|||", Stringimages.ToArray()); } else { for (int i = 0; i < Request.Files.Count; i++) { HttpPostedFileBase file = Request.Files[i]; //Uploaded file //Use the following properties to get file's name, size and MIMEType int fileSize = file.ContentLength; fileName = file.FileName; string mimeType = file.ContentType; System.IO.Stream fileContent = file.InputStream; savedFileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + Path.GetFileName(fileName); file.SaveAs(Path.Combine(Server.MapPath("~/Content/Doc/" + userid), savedFileName)); // Save the file // errormsg.Text += String.Format("{0}<br />", file); Stringimages.Add(savedFileName); } filesnames = string.Join("|||", Stringimages.ToArray()); } int len = storedData.Count; StudyMaterial studyupload = new StudyMaterial(); { studyupload.CmtID = userid; studyupload.CourseID = model.CourseID; studyupload.SemID = model.SemID; studyupload.SM_Description = model.SM_Description; studyupload.SM_Name = model.SM_Name; studyupload.Attachments = savedFileName; studyupload.CreatedDate = DateTime.Now; } dbcontext.StudyMaterials.Add(studyupload); Status = dbcontext.SaveChanges(); if (Status > 0) { msg = "file uploaded Successfully"; } else { msg = "Erro Occuerred"; } } return Json(new { responstext = msg }, JsonRequestBehavior.AllowGet); }
public ActionResult FileUpload(StudyMaterial model) { string msg = ""; int Status = 0; string fileName = ""; string filesnames = ""; var Stringimages = new List<string>(); string savedFileName = ""; DateTime d1 = DateTime.Now; string userID = User.Identity.Name; var userid = (from a in dbcontext.CommitteeMembers_Profile where a.EmailID == userID select a.CmtID).FirstOrDefault(); if (model.SemID != 0 && model.CourseID != 0) { var sdmin = dbcontext.StudyMaterials.Where(s => s.SemID == model.SemID && s.CourseID == model.CourseID && s.CmtID == userid && s.CmtID == userid).FirstOrDefault(); if (sdmin == null) { //Insert New Files in table if (!System.IO.Directory.Exists(Server.MapPath("\\Content\\Doc\\" + userid))) { System.IO.Directory.CreateDirectory(Server.MapPath("\\Content\\Doc\\" + userid)); for (int i = 0; i < Request.Files.Count; i++) { HttpPostedFileBase file = Request.Files[i]; //Uploaded file //Use the following properties to get file's name, size and MIMEType int fileSize = file.ContentLength; fileName = file.FileName; string mimeType = file.ContentType; System.IO.Stream fileContent = file.InputStream; savedFileName = DateTime.Now.Date.ToString("dd/MM/yyyy") + Path.GetFileName(fileName); file.SaveAs(Path.Combine(Server.MapPath("~/Content/Doc/" + userid), savedFileName)); // Save the file // errormsg.Text += String.Format("{0}<br />", file); Stringimages.Add(savedFileName); } filesnames = string.Join("|||", Stringimages.ToArray()); } else { for (int i = 0; i < Request.Files.Count; i++) { HttpPostedFileBase file = Request.Files[i]; //Uploaded file //Use the following properties to get file's name, size and MIMEType int fileSize = file.ContentLength; fileName = file.FileName; string mimeType = file.ContentType; System.IO.Stream fileContent = file.InputStream; savedFileName = DateTime.Now.Date.ToString("dd/MM/yyyy") + Path.GetFileName(fileName); file.SaveAs(Path.Combine(Server.MapPath("~/Content/Doc/" + userid), savedFileName)); // Save the file // errormsg.Text += String.Format("{0}<br />", file); Stringimages.Add(savedFileName); } filesnames = string.Join("|||", Stringimages.ToArray()); } //int len = storedData.Count; StudyMaterial studyupload = new StudyMaterial(); { studyupload.CmtID = userid; studyupload.CourseID = model.CourseID; studyupload.SemID = model.SemID; studyupload.SM_Description = model.SM_Description; studyupload.SM_Name = model.SM_Name; studyupload.Attachments = savedFileName; studyupload.UploadBy = userID; studyupload.CreatedDate = DateTime.Now; } dbcontext.StudyMaterials.Add(studyupload); Status = dbcontext.SaveChanges(); if (Status > 0) { msg = "1"; } else { msg = "0"; } } else { //Update Exisitng tabale data if (!System.IO.Directory.Exists(Server.MapPath("\\Content\\Doc\\" + userid))) { System.IO.Directory.CreateDirectory(Server.MapPath("\\Content\\Doc\\" + userid)); for (int i = 0; i < Request.Files.Count; i++) { HttpPostedFileBase file = Request.Files[i]; //Uploaded file //Use the following properties to get file's name, size and MIMEType int fileSize = file.ContentLength; fileName = file.FileName; string mimeType = file.ContentType; System.IO.Stream fileContent = file.InputStream; savedFileName = DateTime.Now.Date.ToString("dd/MM/yyyy") + Path.GetFileName(fileName); file.SaveAs(Path.Combine(Server.MapPath("~/Content/Doc/" + userid), savedFileName)); // Save the file // errormsg.Text += String.Format("{0}<br />", file); Stringimages.Add(savedFileName); } filesnames = string.Join("|||", Stringimages.ToArray()); } else { // sdmin for (int i = 0; i < Request.Files.Count; i++) { HttpPostedFileBase file = Request.Files[i]; //Uploaded file //Use the following properties to get file's name, size and MIMEType int fileSize = file.ContentLength; fileName = file.FileName; string mimeType = file.ContentType; System.IO.Stream fileContent = file.InputStream; savedFileName = DateTime.Now.Date.ToString("dd/MM/yyyy") + Path.GetFileName(fileName); file.SaveAs(Path.Combine(Server.MapPath("~/Content/Doc/" + userid), savedFileName)); // Save the file // errormsg.Text += String.Format("{0}<br />", file); Stringimages.Add(savedFileName); } filesnames = string.Join("|||", Stringimages.ToArray()); } //data opration perperom here //int len = storedData.Count; var smdat = dbcontext.StudyMaterials.FirstOrDefault(s => s.CmtID == userid && s.SemID == model.SemID && s.CourseID == model.CourseID && s.CmtID == userid); { smdat.CourseID = model.CourseID; smdat.SemID = model.SemID; smdat.SM_Description = model.SM_Description; smdat.SM_Name = model.SM_Name; smdat.Attachments = savedFileName; smdat.UploadBy = userID; smdat.ModifiedDate = DateTime.Now; } var In = dbcontext.SaveChanges(); ; if (In > 0) { msg = "1"; } else { msg = "0"; } // msg = "1"; } } else { msg = "0"; } Bindcourse(); BindSemester(); ViewBag.uploadedfilesdisplay = (from a in dbcontext.StudyMaterials.Where(x => x.SMID != 0) select a).ToList(); return RedirectToAction("FileUpload", "CommitteMember", new { msg }); }
public ActionResult Create(Library library) { string userId = User.Identity.GetUserId(); var userManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(db)); var s = userManager.GetRoles(userId); if (!ModelState.IsValid) { if (s[0].ToString() == "Teacher") { var streamList = new List <SelectListItem>(); var teacher = db.Teachers.Find(userId); var enrollsQuery = (from e in teacher.enrollments orderby e.grade, e.@group ascending select e); foreach (var m in enrollsQuery) { streamList.Add(new SelectListItem { Value = (m.Id).ToString(), Text = m.grade + m.group }); } ViewBag.streamlist = streamList; } if (s[0].ToString() == "Manager") { var user = db.Managers.Find(userId); var streamList = new List <SelectListItem>(); var streamQuery = (from e in user.school.Streams orderby e.name ascending where e.name != "Grade 8 & 9" select e); foreach (var m in streamQuery) { streamList.Add(new SelectListItem { Value = (m.Id).ToString(), Text = m.name }); } ViewBag.streamlist = streamList; } return(View(library)); } if (ModelState.IsValid) { if (s[0].ToString() == "Manager") { var manager = db.Managers.Find(userId); library.School = manager.school; if (library.streamId != null) { var strId = Convert.ToInt64(library.streamId); var stream = manager.school.Streams.ToList().Find(p => p.Id == strId); library.Stream = stream; } } if (s[0].ToString() == "Teacher") { var teacher = db.Teachers.Find(userId); library.School = teacher.school; library.Teacher = teacher; var enrId = Convert.ToInt64(library.enrollId); var Enroll = teacher.enrollments.ToList().Find(p => p.Id == enrId); library.Enrollment = Enroll; } var fileName = Path.GetFileName(library.file.FileName); StudyMaterial studymaterial = new StudyMaterial() { FileName = fileName, Extension = Path.GetExtension(fileName), Id = library.Id, Library = library }; library.StudyMaterial = studymaterial; library._date = DateTime.Now; db.Libraries.Add(library); db.SaveChanges(); var path = Path.Combine(Server.MapPath("~/App_Data/StudyMaterials/"), library.StudyMaterial.Id + fileName); library.file.SaveAs(path); return(RedirectToAction("Index")); } return(View(library)); }