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.
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.
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.
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.
I have implemented pagination on numerous Surface Controllers and it works absolutely fine.
Thanks, I'll do that then.
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.
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:
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
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.
is working on a reply...