How do I do pagination in ASP.NET MVC?
Well, what is the data source? Your action could take a few defaulted arguments, i.e.
ActionResult Search(string query, int startIndex, int pageSize) {...}
defaulted in the routes setup so that startIndex is 0 and pageSize is (say) 20:
routes.MapRoute("Search", "Search/{query}/{startIndex}",
new
{
controller = "Home", action = "Search",
startIndex = 0, pageSize = 20
});
To split the feed, you can use LINQ quite easily:
var page = source.Skip(startIndex).Take(pageSize);
(or do a multiplication if you use "pageNumber" rather than "startIndex")
With LINQ-toSQL, EF, etc - this should "compose" down to the database, too.
You should then be able to use action-links to the next page (etc):
<%=Html.ActionLink("next page", "Search", new {
query, startIndex = startIndex + pageSize, pageSize }) %>
How to perform Pagination in ASP.NET Core MVC
Check the error message, It shows:
https://localhost:5001/Tour/AllTours?pg=2
But in your question, You provide your controller is ToursController
, So try to change asp-controller="Tour"
to asp-controller="Tours"
.
The point I am very confused about is your action name is AllTours
, In general, the corresponding page is AllTours.cshtml
, But you write Index.cshtml
in your question, So I'm not sure if you wrote the question wrong or if you really wrote the page on Index.cshtml
.
The default route template {controller=Home}/{action=Index}/{id?}
, So If you write action AllTours
to page Index.cshtml
, The url will not correctly navigate to that page.
Pagination in ASP.NET MVC Application
The model in the view is LeadingFilterVM
and that model does not contain a property for PageNumber
.
Note that there are other issues with your code, and you need to change the view model to
public class LeadingFilterVM
{
public string Type { get; set; }
public IEnumerable<SelectListItem> TypeList { get; set; }
public IPagedList<LeadingClass> Leadings { get; set; } //IPagedList, not IEnumerable
}
Then you need to change the view to use the Leadings
property which is the IPagedList
Page @(Model.Leadings.PageCount < Model.Leadings.PageNumber ? 0 : Model.Leadings.PageNumber) of @Model.Leadings.PageCount
@Html.PagedListPager(Model.Leadings, page => Url.Action("Leading", new { page, type = Model.Type }))
and change the signature of the method to
public ActionResult Leading(int? type, int? pageNumber)
You also need to change the code to generate the SelectList
to
TypeList = new List<SelectListItem>
{
new SelectListItem{ Text = "General", Value = "General" },
new SelectListItem{ Text = "Advance", Value = "Advance" }
},
or just simply TypeList = new SelectList(new string[]{ "General", "Advance" })
, since you need to post back either "General" or "Advance", not an int
. Having said that, I would recommend that Type
be in int
with a FK relationship to a Types
table in your database.
Note its not clear what your DatabaseHandle.LeadingAll()
and DatabaseHandle(type)
methods return, but they should be returing IQueryable<LeadingClass>
(not IEnumerable<>
or IList<>
) otherwise you defeating the benefits of server side paging. The typical code for getting the records would be
public ActionResult Leading(string type, int? pageNumber)
{
IQueryable<LeadingClass> data = db.LeadingClass;
if (type != null)
{
data = data.Where(x => x.Type == type.Value);
}
LeadingFilterVM model = new LeadingFilterVM
{
....
Leadings = data..ToPagedList(pageNumber ?? 1, 10)
};
return View(model);
}
How to do Pagination in ASP.NET MVC using Sql Data Adapter
The users variable is declared as List here -
var users = obj.ToList();
And again while applying skip IEnumerable is assigned back (Skip will result in IEnumerable), hence the casting error -
users=users.Skip((PageNumber-1)*10);
If you want list, may be just evaluate the result to List as below -
users=users.Skip((PageNumber-1)*10).ToList();
Related Topics
How to Get Memory Available or Used in C#
Replace "\\" with "\" in a String in C#
Catch Exception That Is Thrown in Different Thread
Read Xml Attribute Using Xmldocument
In Unity (C#), Why am I Getting a Nullreferenceexception and How to Fix It
Cannot Use Ref or Out Parameter in Lambda Expressions
Coercing Floating-Point to Be Deterministic in .Net
In Mvvm Should the Viewmodel or Model Implement Inotifypropertychanged
What Does Null! Statement Mean
How to Decode a Url Parameter Using C#
How to Do Pagination in ASP.NET MVC
Servicestack Request Dto Design
Check If Number Is Prime Number
How to Create Bitmap from Byte Array