Advertisement
Guest User

Untitled

a guest
Nov 19th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. using Microsoft.AspNetCore.Mvc;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Week2Opdracht1Administratie17079543.Models; //Pas het MVC-pattern toe door vanuit de Controller een Model mee te geven aan de View.
  5.  
  6. // For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
  7. //17079543 Jurian Klas 2.D week 2 opdracht 1.
  8. namespace Week2Opdracht1Administratie17079543.Controllers
  9. {
  10. //deze Controller gebruikt het Model Student en System.Linq.
  11. //voorbeeld URL: https://localhost:xxxxx/StudentAdministratie/ZoekStudenten/j
  12. public class StudentAdministratieController : Controller
  13. {
  14. private List<Student> StudentenLijst;
  15.  
  16. // GET: /<controller>/
  17. public IActionResult Index()
  18. {
  19. return View();
  20. }
  21.  
  22. public IActionResult ZoekStudenten(string input)
  23. {
  24. //Je hoeft de lijst met studenten nog niet op te halen uit een database. Die mag je hard coded in de juiste action - method definiëren.
  25. StudentenLijst = new List<Student>()
  26. {
  27. new Student(){ Naam = "Jurian", AchterNaam = "PAAA", Leeftijd = 19},
  28. new Student(){ Naam = "Jeffrey", AchterNaam = "JBBB", Leeftijd = 21},
  29. new Student(){ Naam = "Paolo", AchterNaam = "JJJJ", Leeftijd = 23},
  30. new Student(){ Naam = "Muca", AchterNaam = "JAGCXGJ", Leeftijd = 20}
  31. };
  32. //als je niet controleert of input niet null mag zijn geeft VS error.
  33. if (input != null)
  34. {
  35. //gefilterde lijst.
  36. List<Student> StudentenGesorteerd = selecteerAchternaam(input);
  37. //Uitdaging 2: Zorg ervoor dat de View ook laat zien op welke letter is gezocht.
  38. ViewData["gebruikerInput"] = input;
  39. return View(StudentenGesorteerd);
  40. }
  41. else
  42. {
  43. //is voor nu even default..
  44. return ErrorBericht();
  45. }
  46. }
  47.  
  48. //laat de ErrorBericht View zien.
  49. public IActionResult ErrorBericht()
  50. {
  51. return View("ErrorBericht");//verwijst naar de ErrorBericht view.
  52. }
  53.  
  54. //geeft alleen de StudentenLijst waarvan achternaam begint met ingevoerde letter(s).
  55. //return een gefilterde lijst. is NIET hoofdlettergevoelig
  56. public List<Student> selecteerAchternaam(string input)
  57. {
  58. //Uitdaging 1: Gebruik LINQ in combinatie met de String.StartsWith Method voor het zoeken in de lijst.
  59. var result = from Student s in StudentenLijst
  60. where s.AchterNaam.StartsWith(input, System.StringComparison.OrdinalIgnoreCase) //om Case Sensitive te voorkomen.
  61. orderby s.AchterNaam
  62. select s;
  63. List<Student> gesorteerdeLijst = result.ToList();
  64. return gesorteerdeLijst;
  65. }
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement