Home > database >  How to add this Previous/Next Button functionality?
How to add this Previous/Next Button functionality?

Time:06-24

I am migration an application that has "Previous"/"Next" buttons in the Details view to navigate between records. The records to navigate between may have been filtered in the Index view. For example, records 1-5 may have originally all been displayed in the Index view. Then the user filtered the records and only records 1, 3 and 5 are displayed. When the user goes to the Details view of record 3, clicking the "Previous" button navigates to record 1. Clicking the "Next" button navigates to record 5.

In the old solution, a stored procedure is used for filtering, sorting and pagination. A return model of the filtered/unfiltered records is returned and is part of the view model. That is how the filtered record Ids are accessed to navigate between records.

In the new solution, a stored procedure is not used because POCO generator does not generate the return model due to the stored procedure using a temporary table. I have tried setting fmtonly to off, but it does not work and I have read that fmtonly is deprecated.

Instead, a jQuery serverSide datatable and Linq in C# are used.

Currently, I can navigate between all records using the following:

ViewBag.PreviousId = _db.Applications
                        .OrderByDescending(a => a.AppNumber)
                        .Where(a => a.AppNumber < application.AppNumber)
                        .Select(a => a.Id).FirstOrDefault();

ViewBag.NextId = _db.Applications
                    .OrderBy(a => a.AppNumber)
                    .Where(a => a.AppNumber > application.AppNumber)
                    .Select(a => a.Id) 
                    .FirstOrDefault();

How can I navigate between filtered records?

CodePudding user response:

Not sure if i get you right but maybe:

How about take one element and skip position 0,1,2,3,... ?

Next: position 1 Previous: position-1.

.Skip(position)
.Take(1)

https://docs.microsoft.com/en-us/ef/core/querying/pagination#offset-pagination

CodePudding user response:

My solution was to save data.ToList() to a session in the JsonResult method:

Session["HR_Applications"] = data.ToList();.

Then grab the values from the session in the Details method:

var applications = (List<Application>)Session["HR_Applications"];

And lastly, set the previousId and nextId values:

ViewBag.PreviousId = applications.OrderByDescending(a => a.AppNumber).Where(a => a.AppNumber < application.AppNumber).Select(a => a.ApplicationId).FirstOrDefault();

ViewBag.NextId = applications.OrderBy(a => a.AppNumber).Where(a => a.AppNumber > application.AppNumber).Select(a => a.ApplicationId).FirstOrDefault();
  • Related