请你让我知道是否有任何方式来做ASP.NET MVC中的分页,而不使用任何外部nuget包(例如:PageList.mvc)。其实我正在寻找一个服务器端解决方案来做MVC本身的分页。分页ASP.NET MVC中没有使用任何外部nuget包
如果有人知道答案,请帮助我。
感谢 Nishad
请你让我知道是否有任何方式来做ASP.NET MVC中的分页,而不使用任何外部nuget包(例如:PageList.mvc)。其实我正在寻找一个服务器端解决方案来做MVC本身的分页。分页ASP.NET MVC中没有使用任何外部nuget包
如果有人知道答案,请帮助我。
感谢 Nishad
您可以使用WebGrid。它不是NuGet软件包,它是System.Web.Helpers
命名空间的一部分,并提供开箱即用的分页功能。
1.型号:
public class Product
{
public int ID { get; set; }
public string Description { get; set; }
}
2.View:
@model IEnumerable<MVCTutorial.Models.Product>
@{
Layout = null;
WebGrid grid = new WebGrid(Model, canPage: true, canSort: false, rowsPerPage: 2);
}
@grid.GetHtml(
tableStyle: "table",
columns: grid.Columns(
grid.Column("ID", "ID", format: @<text> @item.ID
</text>, style: "p13"),
grid.Column("Description","Description", format: @<text> @item.Description</text>)))
3.Controller:
public class HomeController : Controller
{
public ActionResult GetProducts()
{
var p1 = new Product { ID = 1, Description = "Product 1" };
var p2 = new Product { ID = 2, Description = "Product 2" };
var p3 = new Product { ID = 3, Description = "Product 3" };
var p4 = new Product { ID = 4, Description = "Product 4" };
var products = new List<Product> { p1, p2, p3, p4 };
return View(products);
}
}
谢谢丹尼斯。这对我很有帮助。这是我发现的东西。再次感谢。 – user3796885
嗨丹尼斯,你可以让我知道如何将参数作为ActionResult GetProducts(布尔值)传递给action action方法,当我点击每个分页时。 – user3796885
您可以使用LINQ和传递PAGENUMBER和resultsPerPage到控制器。
var entries = _dbContext.YourTable.OrderBy(e => e.Date).Skip(pageNumber - 1).Take(resultsPerPage);
你的逻辑也应该计算多少页会有通过获取记录的计数为特定的查询,并做一些基本的数学。
这里有一个很好的例子:http://jasonwatmore.com/post/2015/10/30/ASPNET-MVC-Pagination-Example-with-Logic-like-Google.aspx
请注意,模型 - 视图控制器标签是有关该模式的问题。 ASP.NET-MVC实现有一个特定的标签。 –