Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • AmandaEly 123 posts 379 karma points
    May 21, 2015 @ 12:22
    AmandaEly
    0

    What is the best controller type for pagination?

    Hi all,

    My website has a lot of data that will need to be presented in tables (race results and so on). This will need pagination, which implies returning a request to a controller. The controller will need page size and page number. I am reading that surface controllers are intended for forms (llike contact forms) and pagination isn't really a form in that sense. So should I be using a custom controller?

    And of course, I will want to be able to use AJAX for paging at some point but that is another story.

    I am also sure this topic is not new but I have only just heard of custom controllers.

  • Chris Wilson 100 posts 377 karma points
    May 21, 2015 @ 13:45
    Chris Wilson
    0

    I have implemented pagination on numerous Surface Controllers and it works absolutely fine.

  • AmandaEly 123 posts 379 karma points
    May 21, 2015 @ 18:03
    AmandaEly
    0

    Thanks, I'll do that then.

  • AmandaEly 123 posts 379 karma points
    Jun 15, 2015 @ 10:01
    AmandaEly
    0

    Chris,

    could you please give an example of this? I'm pretty lost. Do you, for example, implement your paging in a separate helper with its own controller?

    Can anyone point me to a good example of a plain Surface Controller (no hijacking if it is not needed) which implements paging? I have no problem with getting the data (skip, take and so on), it's just the to and fro of buttons, AnglarJs and Controller that has me in a spin.

  • Chris Wilson 100 posts 377 karma points
    Jun 16, 2015 @ 16:01
    Chris Wilson
    100

    I tend to implement it as a controller action that returns a View pre-populated with the requested page of results - taking the requested page number as a URL parameter for SEO purposes.

    So, for instance if you were making a search results page you could do the following:

    public class SearchController : SurfaceController 
    {
        public ActionResult DisplayResults(int page, string query)
        {
            // Set your search variables
            var pageSize = 10;
    
            //Do the search
            searcher = ExamineManager.Instance.SearchProviderCollection["ExternalSearcher"]
            var results = searcher.Search(query, true)
                                  .Where(r=> r["__IndexType"].Equals("content"))
    
            // Work out how many results to skip
            var resultsToSkip = page > 0 ? (page - 1) * pageSize : 0;
    
            // Take only the results you need
            var resultSet = results.Skip(resultsToSkip)
                                   .Take(pageSize)
    
            return View("SearchResults", resultSet);
        }
    }
    

    Then your View just needs to iterate through the result set and display the results however you want.

    There's probably simpler ways of doing it but I like that you can essentially go anywhere from here, customising the implementation to your current requirements - and still keep the logic in the controller.

    You can further simplify this by putting that logic into the Index method and simply allowing the page to be a simple search engine.

    Let me know if you get stuck anywhere :)

    /Chris

  • AmandaEly 123 posts 379 karma points
    Jun 16, 2015 @ 16:16
    AmandaEly
    0

    Thanks very much. I've taken a solution from a general MVC advice site in the meantime. Each list page has its own pager type.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies