public ActionResult Create(SubjectGradesRecordViewModel subjectGradesRecordViewModel)
        {
            if (ModelState.IsValid)
            {
                var subjectGradesRecord = MapSubjectGradesRecordViewModel_To_SubjectGradesRecord(subjectGradesRecordViewModel);
                _unitOfWork.SubjectGradesRecordRepository.Insert(subjectGradesRecord);
                _unitOfWork.SubjectGradesRecordRepository.Save();
                return RedirectToAction("Index", new { id = subjectGradesRecordViewModel.RegistrationId });
            }

            //ViewBag.SubjectId = new SelectList(_unitOfWork.SubjectRepository.GetAll().Distinct(), "Id", "SubjectCode", subjectGradesRecordViewModel.SubjectId);
            var levelIdOfCurrentRegistration = _unitOfWork.RegistrationRepository.GetById(subjectGradesRecordViewModel.RegistrationId).Degree.LevelId;
            
            subjectGradesRecordViewModel.SubjectsList = new SelectList(_unitOfWork.SubjectRepository.GetAll().Where(s => s.LevelId == levelIdOfCurrentRegistration), "Id", "Name");
            subjectGradesRecordViewModel.PeriodsList = _unitOfWork.PeriodRepository.GetAll().Where(p => p.LevelId == levelIdOfCurrentRegistration);
            subjectGradesRecordViewModel.GradeViewModels = new List<GradeViewModel>();

            return View(subjectGradesRecordViewModel);
        }
        private SubjectGradesRecordViewModel MapSubjectGradesRecord_To_SubjectGradesRecordViewModel(SubjectGradesRecord subjectGradesRecord)
        {
            var subjectGradesRecordViewModel = new SubjectGradesRecordViewModel()
            {
                RegistrationId = subjectGradesRecord.Registration.Id,
                SubjectId = subjectGradesRecord.Subject.Id,
                LevelId = subjectGradesRecord.Registration.Degree.LevelId,
                StudentFullName = subjectGradesRecord.Registration.Student.FullName,
                GradeViewModels = new List<GradeViewModel>()
            };

            foreach (var grade in subjectGradesRecord.Grades)
            {
                subjectGradesRecordViewModel.GradeViewModels.Add(new GradeViewModel
                {
                    Id = grade.Id,
                    PeriodId = grade.PeriodId,
                    PeriodName = grade.Period.Name,
                    GradeValue = grade.GradeValue
                });
            }

            return subjectGradesRecordViewModel;
        }
 /// <summary>
 /// Creates a SubjectGradesRecord for the specified registration id.
 /// </summary>
 /// <param name="registrationId">The current registration id to which this SubjectGradesRecord is related.</param>
 /// <returns></returns>
 public ActionResult Create(int registrationId)
 {
     var currentRegistration = _unitOfWork.RegistrationRepository.GetById(registrationId);
     if (currentRegistration == null)
     {
         return HttpNotFound();
     }
     var levelIdOfCurrentRegistration = currentRegistration.Degree.LevelId;
     
     var subjectGradesRecordViewModel = new SubjectGradesRecordViewModel
     {
         //Id = 0,
         RegistrationId = registrationId,
         SubjectsList = new SelectList(_unitOfWork.SubjectRepository.GetAll().Where(s => s.LevelId == levelIdOfCurrentRegistration) , "Id", "Name"),
         PeriodsList = _unitOfWork.PeriodRepository.GetAll().Where(p => p.LevelId == levelIdOfCurrentRegistration),
         StudentFullName = currentRegistration.Student.FullName,
         GradeViewModels = new List<GradeViewModel>()
     };
     
     return View(subjectGradesRecordViewModel);
 }
        private SubjectGradesRecord MapSubjectGradesRecordViewModel_To_SubjectGradesRecord(SubjectGradesRecordViewModel subjectGradesRecordViewModel)
        {
            var subjectGradesRecord = _unitOfWork.SubjectGradesRecordRepository.GetById(subjectGradesRecordViewModel.Id);

            // For editing existing grade record
            if (subjectGradesRecord != null)
            {
                subjectGradesRecord.SubjectId = subjectGradesRecordViewModel.SubjectId;

                foreach (var gradeViewModel in subjectGradesRecordViewModel.GradeViewModels)
                {
                    var grade = _unitOfWork.GradeRepository.GetById(gradeViewModel.Id);
                    grade.GradeValue = gradeViewModel.GradeValue;
                }
            }
            else // For creating new grade record
            {
                subjectGradesRecord =  new SubjectGradesRecord
                {
                    Id = subjectGradesRecordViewModel.Id, 
                    Registration = _unitOfWork.RegistrationRepository.GetById(subjectGradesRecordViewModel.RegistrationId),
                    SubjectId = subjectGradesRecordViewModel.SubjectId,
                    Grades = new List<Grade>()
                };

                foreach (var grade in subjectGradesRecordViewModel.GradeViewModels)
                {
                    subjectGradesRecord.Grades.Add(new Grade
                    {
                        PeriodId = grade.PeriodId,
                        GradeValue = grade.GradeValue
                    });
                }
            }
            
            return subjectGradesRecord;
        }