Asp.net mvc 4 如何在MVC4中实现分页和排序

Asp.net mvc 4 如何在MVC4中实现分页和排序,asp.net-mvc-4,razor,paging,Asp.net Mvc 4,Razor,Paging,很抱歉今天早上打扰你们,但我有一个问题想问你们大家。如何将分页和排序添加到网页MVC4中?我搜索了很多这样的例子,但在我的项目中没有找到 在控制器中: public ActionResult Index(int? Id, String Name) { IQueryable<ProductSubcategory> list = null; if (Id == null) {

很抱歉今天早上打扰你们,但我有一个问题想问你们大家。如何将分页和排序添加到网页MVC4中?我搜索了很多这样的例子,但在我的项目中没有找到

控制器
中:

public ActionResult Index(int? Id, String Name)
        {

            IQueryable<ProductSubcategory> list = null;
            if (Id == null)
            {
                list = BikesDB.ProductSubcategories;
             }
            else
            {
                int id = Id.Value;
                list = BikesDB.ProductSubcategories.Where(m => m.ProductSubcategoryID == id && m.NameofBike == Name);
            }

            var bikes = list.Where(m => m.isSelected == true)
                .AsEnumerable().Select(p => new Bike { Id = p.ProductSubcategoryID, Name = p.NameofBike });
            var viewModel = new CategoriesIndexViewModel
            {
                NumberOfModel = bikes.Count(),
                NameofBike = bikes.Select(b=>b.Name).ToList(),
                Bikes = bikes.ToList()
            };
            return this.View(viewModel);
        }

有人对我怎么做有什么建议吗?多谢各位

您应该第一次获得前10条或20条记录,在某些事件中,从用户处获得下20条记录:

var bikes = list.Where(m => m.isSelected == true)
                .AsEnumerable()
                .Select(p => new Bike { Id = p.ProductSubcategoryID, 
                                        Name = p.NameofBike })
                .Take(20);
然后单击next按钮,根据前20条记录的最后一个id获取下20条记录


第二种方法是使用,它将应用客户端分页到记录,但它将在一次调用中获得所有记录,如果您有大量记录,这不是一种好方法。

到目前为止您尝试了什么?具体问题是什么?@Péter我正在尝试将页面添加到网页中,例如,我的
主页中有100个产品,现在我想在一个页面中将其划分为15或20个产品,总共有5个页面。你知道我的意思吗?谢谢你的回答,但我想要的是这样的感谢分享,我自己也会在需要的时候使用
var bikes = list.Where(m => m.isSelected == true)
                .AsEnumerable()
                .Select(p => new Bike { Id = p.ProductSubcategoryID, 
                                        Name = p.NameofBike })
                .Take(20);