public ViewModels.Admin.Edit LoadEditViewModel(int id) { using (var conn = new dbEntities()) { var ret = new ViewModels.Admin.Edit() { subjectDescription = conn.SubjectDescriptions.FirstOrDefault(s => s.ID == id) }; if (ret.subjectDescription == null) { throw new Exception("Invalid subject description record."); } ret.ExamPapers = conn.SubjectExamPapers.Where(s => s.SubjectDescription_ID == ret.subjectDescription.ID).ToList(); ret.subject = ret.subjectDescription.Subject; return(ret); } }
public ViewModels.Admin.Edit LoadEditViewModel(string subject) { var ret = new ViewModels.Admin.Edit() { subject = subject.Replace("_", " ").Trim() }; var matched = false; foreach (var dpt in bat.logic.Constants.Subjects.Departments) { if (dpt.Subjects.Contains(ret.subject)) { matched = true; } } if (!matched) { throw new Exception("Invalid subject"); } using (var conn = new dbEntities()) { ret.subjectDescription = conn.SubjectDescriptions.FirstOrDefault(s => s.Subject == subject) ?? new SubjectDescription() { Subject = subject }; if (ret.subjectDescription.ID > 0) { ret = LoadEditViewModel(ret.subjectDescription.ID); } } return(ret); }