//this method is used to promote students from one class to other public async Task<bool> promoteStudents(PromotionsSelectVM model) { //This mehod is called to check if next academic year from the next exist if (!CheckNextAcademicYear()) { return false; } //create a transaction for multiple student records to inserted into studentCurrentYear table //transaction makes this process atomic using (var dbtrans = db.Database.BeginTransaction()) { //using try catch block to rollback transaction in case of error try { //iterate through studentlist object of PromotionsSelectVM foreach (var studentlist in model.StudentLists) { // if detained is false then if (studentlist.Detained == false) { //fetch the active academicyear record from database StudentCurrentYear currentacademicyear = db.StudentCurrentYears.Where(scy => scy.StudentRefID == studentlist.StudentRefID && scy.Active == true).SingleOrDefault(); //Change the active flag to false currentacademicyear.Active = false; //add record to be updated by entityframework db.StudentCurrentYears.Attach(currentacademicyear); db.Entry(currentacademicyear).State = EntityState.Modified; //Create a new record, inserted for newly promoted class StudentCurrentYear addStudentCurrentYear = new StudentCurrentYear { //get the next academicyear available in the academicyear table //by using method getNextyearfromCurrent by passing students academicyear //and current school id's AcademicYearRefID = getNextYearfromCurrent(studentlist.AcademicYearRefID, studentlist.SchoolRefID), ClassRefID = getClassID(model.ClassTo), SchoolRefID = studentlist.SchoolRefID, StudentRefID = studentlist.StudentRefID, Active = true }; db.StudentCurrentYears.Add(addStudentCurrentYear); } } await db.SaveChangesAsync(); dbtrans.Commit(); return true; } catch (Exception e) { dbtrans.Rollback(); return false; } } }
public async Task<ActionResult> PromotionStudentListPartial(PromotionsSelectVM promotionVM ) { //ViewBag.Success = "Student Promoted"; //TempData["studentVM"] = _service.getPromotionList(model.ClassFrom, model.ClassTo, 1); if(await Task.Run(() => _service.promoteStudents(promotionVM,User.Identity.GetUserName()))== false) { TempData["academicyearMsg"] = "No next academic year from current userprefered academic year"; //buy passing 0 to getpromotionlist the data returned is empty studentlist in PromotionSelectVM // which is stored in Tempdata to be accesed by PromotedStudentList view TempData["studentVM"] = _service.getPromotionList(promotionVM.ClassFrom, promotionVM.ClassTo, 0,User.Identity.GetUserName()); } else { TempData["academicyearMsg"] = "Following students are promoted"; //buy passing 2 to getpromotionlist the data returned is promoted to next year students in studentlist of PromotionSelectVM // which is stored in Tempdata to be accesed by PromotedStudentList view TempData["studentVM"] = _service.getPromotionList(promotionVM.ClassFrom, promotionVM.ClassTo, 2,User.Identity.GetUserName()); } return RedirectToAction("PromotedStudentList"); }
public ActionResult PromotionsSelectClass(PromotionsSelectVM promotions) { Tuple<int, int> currentuserpreference = _service.getUserCurrentSchool(_service.getDBContext()); OsisContext dbc = _service.getDBContext(); // store list from db in variable to used commonly when model fails or valid var dropdownlist = new SelectList(dbc.SchoolClasses.AsNoTracking().Where(sch => sch.SchoolRefID == currentuserpreference.Item1).Select(x => new { ClassOrder = x.ClassOrder + "" + x.ClassID, x.ClassName }), "ClassOrder", "ClassName"); if(ModelState.IsValid) { ViewBag.ClassRefID = dropdownlist; //passing 1 paramter to getpromotionlist we get list of studentS for userpreference school and academicyear in a class return View(_service.getPromotionList(promotions.ClassFrom,promotions.ClassTo,1)); } ViewBag.ClassRefID = dropdownlist; //passing no parameter value for thrid parameter(ie valid) dafault to zero, hence a empty dataset is //returned(studentlist of PromotionSelectVM) return View(_service.getPromotionList(promotions.ClassFrom,promotions.ClassTo)); }
//by changing the value schoolredid and classid, we can get different datasets private PromotionsSelectVM getPromotionlistCommon(int classfrom,int classto,int schoolrefid,int academicyearid,int classid) { var studentlist = db.AjaxStudentLists.AsNoTracking().Where(s => s.SchoolRefID == schoolrefid && s.ClassRefID == classid && s.AcademicYearRefID == academicyearid && s.Active == true).ToList(); IList<StudentListVM> studentVM = Mapper.Map<IList<StudentListVM>>(studentlist); PromotionsSelectVM PVM = new PromotionsSelectVM() { ClassFrom = classfrom, ClassTo = classto, StudentLists = studentVM }; return PVM; }