ASP.NET MVC PageList 分页

ASP.NET MVC 有一个分页控件-PageList,能够很好地实现分页效果。

1.Model

    public class VirtualMachine
    {
        public int Id { set; get; }

        public string HostName { set; get; }

        public string IPAddress { set; get; }

        public string Owner { set; get; }

        public string State { set; get; }

        public DateTime ExpiredDate { set; get; }

        public string IsAzure { set; get; }
    }

2.数据源

public class CloudResourceDatasource
    {
        public static IEnumerable<VirtualMachine> GetAll()
        {
            return new List<VirtualMachine>
            {
                new VirtualMachine{ Id = 1, HostName = "Azure", IPAddress = "192.168.21.1", Owner = "justin", State = "ON", ExpiredDate = DateTime.Now, IsAzure = "true"},
                new VirtualMachine{ Id = 2, HostName = "Azure", IPAddress = "192.168.21.1", Owner = "justin", State = "ON", ExpiredDate = DateTime.Now, IsAzure = "true"},
                new VirtualMachine{ Id = 3, HostName = "Azure", IPAddress = "192.168.21.1", Owner = "justin", State = "ON", ExpiredDate = DateTime.Now, IsAzure = "true"},
                new VirtualMachine{ Id = 4, HostName = "Azure", IPAddress = "192.168.21.1", Owner = "justin", State = "ON", ExpiredDate = DateTime.Now, IsAzure = "true"},
                new VirtualMachine{ Id = 5, HostName = "Azure", IPAddress = "192.168.21.1", Owner = "justin", State = "ON", ExpiredDate = DateTime.Now, IsAzure = "true"},
                new VirtualMachine{ Id = 6, HostName = "Azure", IPAddress = "192.168.21.1", Owner = "justin", State = "ON", ExpiredDate = DateTime.Now, IsAzure = "true"},
                new VirtualMachine{ Id = 7, HostName = "Azure", IPAddress = "192.168.21.1", Owner = "justin", State = "ON", ExpiredDate = DateTime.Now, IsAzure = "true"},
                new VirtualMachine{ Id = 8, HostName = "Azure", IPAddress = "192.168.21.1", Owner = "justin", State = "ON", ExpiredDate = DateTime.Now, IsAzure = "true"},
                new VirtualMachine{ Id = 9, HostName = "Azure", IPAddress = "192.168.21.1", Owner = "justin", State = "ON", ExpiredDate = DateTime.Now, IsAzure = "true"},
                new VirtualMachine{ Id = 10, HostName = "Azure", IPAddress = "192.168.21.1", Owner = "justin", State = "ON", ExpiredDate = DateTime.Now, IsAzure = "true"},
                new VirtualMachine{ Id = 11, HostName = "Azure", IPAddress = "192.168.21.1", Owner = "justin", State = "ON", ExpiredDate = DateTime.Now, IsAzure = "true"},
                new VirtualMachine{ Id = 12, HostName = "Azure", IPAddress = "192.168.21.1", Owner = "justin", State = "ON", ExpiredDate = DateTime.Now, IsAzure = "true"},
                new VirtualMachine{ Id = 13, HostName = "Azure", IPAddress = "192.168.21.1", Owner = "justin", State = "ON", ExpiredDate = DateTime.Now, IsAzure = "true"},
                new VirtualMachine{ Id = 14, HostName = "Azure", IPAddress = "192.168.21.1", Owner = "justin", State = "ON", ExpiredDate = DateTime.Now, IsAzure = "true"}
            };
        }
    }
3.控制器

public ActionResult Index(int page = 1)
        {
            const int pagesize = 3;
            var data = CloudResourceDatasource.GetAll()
                .OrderBy(p => p.Id).ToPagedList(page, pagesize);
            return View(data);
        }
4.页面

@{
    ViewBag.Title = "Home Page";
}

@using PagedList
@using PagedList.Mvc

@model IEnumerable<MvcPagerDemo.Models.VirtualMachine>

<table>
    @foreach (var item in Model)
    {
        <tr>
            <td>@item.Id</td>
            <td>@item.IPAddress</td>
        </tr>
    }
</table>
@Html.PagedListPager((IPagedList)Model,page=>Url.Action("Index",new {page}))
5.分页效果

注意:这个效果需要一个pagelist.css,如果通过nuget添加会自动添加进去,否则需要手动添加。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值