PersonController.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using Microsoft.AspNetCore.Mvc;
  2. namespace HallOfFame.Controllers
  3. {
  4. public class PersonController : Controller
  5. {
  6. private readonly PersonDBContext _db;
  7. public PersonController(PersonDBContext db)
  8. {
  9. _db = db;
  10. }
  11. public IActionResult Index()
  12. {
  13. IEnumerable<Person> objPersonList = _db.Persons;
  14. return View(objPersonList);
  15. }
  16. public IActionResult ViewSkill()
  17. {
  18. IEnumerable<Skill> objPersonList = _db.Skills;
  19. return View(objPersonList);
  20. }
  21. public IActionResult Create()
  22. {
  23. return View();
  24. }
  25. //POST
  26. [HttpPost]
  27. [ValidateAntiForgeryToken]
  28. public IActionResult Create(Person obj)
  29. {
  30. _db.Persons.Add(obj);
  31. _db.SaveChanges();
  32. return RedirectToAction("Index");
  33. }
  34. public IActionResult Edit(long? id)
  35. {
  36. if (id==null || id == 0)
  37. {
  38. return NotFound();
  39. }
  40. var person = _db.Persons.Find(id);
  41. if (person == null)
  42. {
  43. return NotFound();
  44. }
  45. return View(person);
  46. }
  47. public IActionResult Result(long? id)
  48. {
  49. if (id == null || id == 0)
  50. {
  51. return NotFound();
  52. }
  53. var con = _db.ConPersonSkills.Where(x=>x.PersonId == id).ToList();
  54. if (con == null)
  55. {
  56. return NotFound();
  57. }
  58. ViewBag.data = con;
  59. return View(con);
  60. }
  61. //POST
  62. [HttpPost]
  63. [ValidateAntiForgeryToken]
  64. public IActionResult Edit(Person obj)
  65. {
  66. _db.Persons.Update(obj);
  67. _db.SaveChanges();
  68. return RedirectToAction("Index");
  69. }
  70. }
  71. }