asp.net mvc 简单分页实现

本文介绍了一个简单的ASP.NET MVC应用中的分页实现方法。通过传递当前页码参数到控制器,利用UserService获取分页后的用户列表,并返回视图展示。同时展示了如何在视图中通过URL.Action方法设置分页链接。

分页首先最重要的两个参数就是当前页和页面大小。

在Controller中:

              <summary>
               分页
              </summary>
              <returns></returns>
          public ActionResult Test(string index)
           {
            if (string.IsNullOrEmpty(index)) //index为当前页,作为参数进行传递
                 index = "1";
              List<User> userlist;   //list数据集
              int totalCount = 0;
              userService = new UserService();                            //GetModelList为分页方法,已经进行了封装,10为固定的页面大小
              userlist = userService.ConvertListType<User>(userService.GetModelList(string.Empty, "RegTime DESC", Int32.Parse(index), 10, ref totalCount));    
              return View(userlist);
            }

只注意Action中index的传递即可。

<h2>fen ye 信息查询</h2>
@model List<MedCrab.Core.Model.APP.User>
    <table>
        <tr>
            <th>
                ID:
            </th>
            <th>
                name:
            </th>
            <th>
                phone:
            </th>
            <th>
                sex:
            </th>
        </tr>

        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @item.ID
                </td>
                <td>
                    @item.fNickName
                </td>
                <td>
                    @item.Phone
                </td>
                <td>
                    @item.fSex
                </td>
            </tr>
        }
    </table>
<ul>@Url.Action("Test", new { index=1 })
    <li style='float:left;margin-left:20px;'>
        <a style='cursor:pointer;' href="">1</a>
        <a style='cursor:pointer;' href="@Url.Action("Test", new { index=2 })">2</a>
        <a style='cursor:pointer;' href="@Url.Action("Test", new { index=3 })">3</a>
        <a style='cursor:pointer;' href="@Url.Action("Test", new { index=4 })">4</a>
    </li>
</ul>
a标签中使用Url.Action方法进index的交互。@model强类型进行list数据集的接收。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值