ahmed elbarbary

ahmed elbarbary

  • NA
  • 1.6k
  • 281.4k

How to add functionality to buttons( Next previous first las

Jan 6 2019 2:45 PM
How to add functionality to buttons( Next previous first last) navigation buttons to primary key field using mvc
==========Problem==========
Cannot add functions to Next previous last first for create action using repository pattern based on EmployeeId field
==========Example==========
Employee Id : 5 as last record
IF create action view get loaded it must show max+1 for Employee Id meaning it will show 6
IF Click NextButton become 6
IF Click previousButton become 4 and get another data related as name age,etc...
IF Click LastButton become 5 and get another data related as name age,etc...
IF Click FirstButton become 1 and get another data related as name age,etc...
==========Details Code===========
I work in employee controller in action create
View action create have 4 buttons (First - Last - Next - Previous)
when create action loaded it will come with max number + 1 for EmployeeId
and after that using button previous next last first )
===========Employee Controller Have Create Action=====
  1. public class EmployeesController : Controller  
  2. {  
  3. private readonly IEmployees _context;  
  4.   
  5. public EmployeesController(IEmployees context)  
  6. {  
  7. _context = context;  
  8. }  
  9.   
  10. // GET: Employees  
  11. public async Task<IActionResult> Index()  
  12. {  
  13. return View(await _context.GetAllAsyn());  
  14. }  
  15.   
  16. // GET: Employees/Create  
  17. public IActionResult Create()  
  18. {  
  19. How to call functions below  
  20. // var maxId = maxid+1;  
  21. getNext()  
  22. getprevious()  
  23. getlast()  
  24. getfirst()  
  25. return View();  
  26. }  
  27. ===============Generic Repository Pattern(required function I dont know======  
  28. public class EFRepository<T> : IRepository<T> where T : class  
  29. {  
  30. protected TabDbContext _context { getset; }  
  31. public EFRepository(TabDbContext context)  
  32. {  
  33. _context = context;  
  34. }  
  35. getNext(){  
  36. what implementation write  
  37. }  
  38. getprevious()  
  39. {  
  40. what implementation write  
  41. }  
  42. getlast()  
  43. {  
  44. what implementation write  
  45. }  
  46. getfirst()  
  47. {  
  48. what implementation write  
  49. }  
  50. public IQueryable<T> GetAll()  
  51. {  
  52. return _context.Set<T>();  
  53. }  
  54.   
  55. public virtual async Task<ICollection<T>> GetAllAsyn()  
  56. {  
  57.   
  58. return await _context.Set<T>().ToListAsync();  
  59. }  
  60.   
  61. public virtual T Get(int id)  
  62. {  
  63. return _context.Set<T>().Find(id);  
  64. }  
  65.   
  66. public virtual async Task<T> GetAsync(int id)  
  67. {  
  68. return await _context.Set<T>().FindAsync(id);  
  69. }  
  70.   
  71. public virtual T Find(Expression<Func<T, bool>> match)  
  72. {  
  73. return _context.Set<T>().SingleOrDefault(match);  
  74. }  
  75.   
  76. public virtual async Task<T> FindAsync(Expression<Func<T, bool>> match)  
  77. {  
  78. return await _context.Set<T>().SingleOrDefaultAsync(match);  
  79. }  
  80.   
  81. public ICollection<T> FindAll(Expression<Func<T, bool>> match)  
  82. {  
  83. return _context.Set<T>().Where(match).ToList();  
  84. }  
  85.   
  86. public async Task<ICollection<T>> FindAllAsync(Expression<Func<T, bool>> match)  
  87. {  
  88. return await _context.Set<T>().Where(match).ToListAsync();  
  89. }  
  90.   
  91. }  
  92. =====Buttons Navigation on Create View Of Employee Controller====  
  93. <div class="title_of_div">  
  94. <button id="BtnFirst" style="display:inline"><b>First</b></button>  
  95. <button id="BtnNext" style="display:inline"><b>Next</b></button>  
  96. <button id="BtnPrevious" style="display: inline"><b>Previous</b></button>  
  97. <button id="BtnLast" style="display: inline"><b>Last</b></button>  
  98. </div>  
  99. view create controlls  
  100. ===========Create Action Get==========  
  101. <form asp-action="Create">  
  102.   
  103. <div class="form-group">  
  104. <label asp-for="EmployeeId" class="control-label"></label>  
  105. <input asp-for="EmployeeId" class="form-control" />  
  106. <span asp-validation-for="EmployeeId" class="text-danger"></span>  
  107. </div>  
  108. <div class="form-group">  
  109. <label asp-for="BranchCode" class="control-label"></label>  
  110. <input asp-for="BranchCode" class="form-control" />  
  111. <span asp-validation-for="BranchCode" class="text-danger"></span>  
  112. </div>  
  113. <div class="form-group">  
  114. <label asp-for="EmployeeName" class="control-label"></label>  
  115. <input asp-for="EmployeeName" class="form-control" />  
  116. <span asp-validation-for="EmployeeName" class="text-danger"></span>  
  117. </div>  
  118. </form>  
  119. </div>  
  120. </div>  
  121. ============Model related data=============  
  122. public class Employee  
  123. {  
  124. [Key]  
  125. public int EmployeeId { getset; }  
  126. public int BranchCode { getset; }  
  127. public string EmployeeName { getset; }  
  128. }  
=======technology Used============
I work in visual studio 2017 asp.net core 2.1 sql server 2012 using repository pattern generic

Answers (3)