public ActionResult CreateStudent(int? id) { StudentCreateStudentVM model = new StudentCreateStudentVM(); CourseRepository cRepo = new CourseRepository(); List<SelectListItem> list = new List<SelectListItem>(); if (id != null) { var courses = cRepo.GetAll(filter: c => c.ID == id.Value).FirstOrDefault(); list.Add(new SelectListItem() { Text = courses.Name + " || " + courses.FKNumber, Value = courses.ID.ToString() }); } else { var courses = cRepo.GetAll(); foreach (var item in courses) { list.Add(new SelectListItem() { Text = item.Name + " || " + item.FKNumber, Value = item.ID.ToString() }); } } model.CoursesList = list; return View(model); }
public ActionResult CreateStudent(StudentCreateStudentVM model, int? id) { Student student = new Student(); Course course = new Course(); UnitOfWork unitOfWork = new UnitOfWork(); UserRepository<Student> sRepo = new UserRepository<Student>(); CourseRepository courseRepo = new CourseRepository(); if (!ModelState.IsValid) { List<SelectListItem> list = new List<SelectListItem>(); if (id != null) { var courses = courseRepo.GetAll(filter: c => c.ID == id.Value).FirstOrDefault(); list.Add(new SelectListItem() { Text = courses.Name + " || " + courses.FKNumber, Value = courses.ID.ToString() }); } else { var courses = courseRepo.GetAll(); foreach (var item in courses) { list.Add(new SelectListItem() { Text = item.Name + " || " + item.FKNumber, Value = item.ID.ToString() }); } } model.CoursesList = list; return View(model); } int courseID = Convert.ToInt32(model.selectedValueID); course = courseRepo.GetAll(filter: c => c.ID == courseID).FirstOrDefault(); var fkNumberCount = (from t in sRepo.GetAll(filter: x => x.FacultiNumber.Contains(course.FKNumber)).OrderByDescending(s => s.FacultiNumber) select t).FirstOrDefault(); student.FirstName = model.FirstName; student.LastName = model.LastName; if (fkNumberCount == null) { student.FacultiNumber = (Convert.ToInt32(course.FKNumber) * 1000 + 1).ToString(); } else { student.FacultiNumber = (Convert.ToInt32(fkNumberCount.FacultiNumber) + 1).ToString(); } student.CourseID = courseID; student.Active = true; Passphrase hash = PasswordHasher.Hash("password"); student.Hash = hash.Hash; student.Salt = hash.Salt; student.Username = "******" + student.FacultiNumber; sRepo.Save(student); if (id != null) { return RedirectToAction("CreateStudent", "Student", new { id = id }); } return RedirectToAction("ListStudents", "Student"); }