public ActionResult Index(string search="") { StudentIndexViewModel sivm = new StudentIndexViewModel { Students = studentsRepository.All.ToList(), CompetencyHeaders = competencyHeadersRepository.All.ToList() }; return View(sivm); }
public ActionResult Index(string searchString = "") { IEnumerable<Student> students = _studentsRepository.All; if (!string.IsNullOrEmpty(searchString) && searchString.Contains(" ")) { int spaceIndex = searchString.IndexOf(" "); int toLastIndex = searchString.Length - spaceIndex; var substring1 = searchString.Substring(0, spaceIndex); var substring2 = searchString.Substring(spaceIndex + 1, toLastIndex - 1); students = _studentsRepository.All.Where(s => s.Firstname == (substring1) && s.Lastname == (substring2)); } else if (!string.IsNullOrEmpty(searchString)) { students = _studentsRepository.All.Where(s => s.Firstname.Contains(searchString) || s.Lastname.Contains(searchString)).OrderBy(x => x.Lastname); } StudentIndexViewModel viewModel = new StudentIndexViewModel { Students = students.ToList(), CompetencyHeaders = _competencyHeadersRepository.All.ToList() }; return View(viewModel); }