public ActionResult AssignSubject(GradeVsSubject model) { try { GradeVsSubject classSubject = model; classSubject.Grade = _context.Grade.FirstOrDefault(x => x.Id == model.GradeId); classSubject.Subject = _context.Subject.FirstOrDefault(x => x.Id == model.SubjectId); classSubject.Description = model.Grade.Grade1 + "'s " + model.Subject.SubjectName + " subject."; if (ValidateClassSubject(model)) { _context.GradeVsSubject.Add(classSubject); _context.SaveChanges(); TempData["ResultCode"] = (int)ResultCode.SUCCESS; TempData["ResultMessage"] = "Added Successfully"; return(RedirectToAction("Index")); } else { TempData["ResultCode"] = (int)ResultCode.FAILED; TempData["ResultMessage"] = "Already Exists"; return(RedirectToAction("Index")); } } catch (Exception e) { TempData["ResultCode"] = (int)ResultCode.FAILED; TempData["ResultMessage"] = e.ToString(); return(RedirectToAction("Index")); } }
private bool ValidateClassSubject(GradeVsSubject model) { List <GradeVsSubject> classSubjects = _context.GradeVsSubject.Where(x => x.GradeId == model.GradeId).ToList(); foreach (var item in classSubjects) { if (item.Subject == model.Subject) { return(false); } else { return(true); } } return(true); }