public ActionResult DeleteConfirmed(int id) { StudentAward studentAward = db.StudentAwards.Find(id); db.StudentAwards.Remove(studentAward); db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Edit([Bind(Include = "StudentAward_ID,Student_ID,Award_ID")] StudentAward studentAward) { if (ModelState.IsValid) { db.Entry(studentAward).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.Award_ID = new SelectList(db.Awards, "Award_ID", "Reward_Comment", studentAward.Award_ID); return(View(studentAward)); }
// GET: StudentAwards/Details/5 public ActionResult Details(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } StudentAward studentAward = db.StudentAwards.Find(id); if (studentAward == null) { return(HttpNotFound()); } return(View(studentAward)); }
// GET: StudentAwards/Edit/5 public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } StudentAward studentAward = db.StudentAwards.Find(id); if (studentAward == null) { return(HttpNotFound()); } ViewBag.Award_ID = new SelectList(db.Awards, "Award_ID", "Reward_Comment", studentAward.Award_ID); return(View(studentAward)); }
public ActionResult Create([Bind(Include = "Award_ID,Staff_ID,Num_Points,RewardCategory_ID,Reward_Comment,Students,StudentCount,AwardDate,Subject_ID")] Award award) { if (ModelState.IsValid) { PopulateSubjectDropDownList(award.Subject_ID); PopulateCategoryDropDownList(award.RewardCategory_ID); //get staff userID int userID = Convert.ToInt32(SessionPersister.UserID); award.Staff_ID = userID; award.AwardDate = DateTime.Today; //find staff remaining points int remainingPoints = db.Accounts.Where(acc => acc.User_ID.Equals(userID)).OfType <Staff>().FirstOrDefault().Remaining_Points; int points = award.Num_Points; int checkPoints = points * award.StudentCount; //if staff has enough points if (remainingPoints >= checkPoints) { //update staff with new points value Staff staff = db.Accounts.OfType <Staff>().SingleOrDefault(s => s.User_ID == userID); staff.Remaining_Points = remainingPoints - checkPoints; staff.Awards.Add(award); SessionPersister.RemainingPoints = staff.Remaining_Points; db.Entry(staff).State = EntityState.Modified; //save award db.Awards.Add(award); db.SaveChanges(); //for number of students selected for (var i = 0; i < award.StudentCount; i++) { int studentID = Convert.ToInt32(award.Students[i]); //create new StudentAwards StudentAward sAward = new StudentAward(); sAward.Award_ID = award.Award_ID; sAward.Student_ID = studentID; db.StudentAwards.Add(sAward); //update student with new points value Student student = db.Accounts.OfType <Student>().SingleOrDefault(s => s.User_ID == studentID); int currentBalance = student.Balance + points; int totalPoints = student.Total_Points + points; student.Balance = currentBalance; student.Total_Points = totalPoints; db.Entry(student).State = EntityState.Modified; } db.SaveChanges(); return(RedirectToAction("Index")); } else { return(RedirectToAction("Index", new { message = "Fail" })); } } return(View(award)); }