MVC简单分页

对Car汽车表分页

实现简单分页,放在这里方便查看回顾,自定义每页几条有点问题,有待完善······

1.新建mvc项目

2.添加linq to sql 数据库连接

3.添加CarBF类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Mvc简单分页.Models
{
    public class CarBF
    {
        private MyDBDataContext _Context = new MyDBDataContext();
        //获取总页数
        public int GetPageCount(int pageSize)
        {
            int rowsCount = _Context.Car.Count();
            int pageCount = (int)Math.Ceiling(1.0 * rowsCount / pageSize);//取天花板数
            return pageCount;
        }
        //获取指定页数据
        public List<Car> Select(int PageSize, int PageNo)
        {
            var query = _Context.Car.Skip(PageSize * (PageNo - 1)).Take(PageSize);
            return query.ToList();
        }
    }
}

  添加HomeController控制器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Mvc简单分页.Models;

namespace Mvc简单分页.Controllers
{
    public class HomeController : Controller
    {
        private int PAGESIZE = 5;//每页多少条数据
        //
        // GET: /Home/

        public ActionResult Index(int? id)
        {
            ViewBag.pagesize = PAGESIZE;
            if (id == null)
            {
                id = 1;
            }
            List<Car> list = new CarBF().Select(PAGESIZE, id.Value);//找出当前数据
            int pageCount = new CarBF().GetPageCount(PAGESIZE);//找出总页数
            int nextPageNo = id.Value >= pageCount ? pageCount : id.Value + 1;//计算下一页页号
            int prevPageNo = id.Value == 1 ? 1 : id.Value - 1;//计算上一页页号
            //使用viewbag带到视图去
            ViewBag.NextPageNo = nextPageNo;
            ViewBag.PrevPageNo = prevPageNo;
            ViewBag.PageCount = pageCount;//总页数
            ViewBag.PageNo = id;//当前页号


            //下拉列表显示页数需要的selectlist数据
            List<int> listPage = new List<int>();
            for (int i = 1; i <= pageCount; i++)
            {
                listPage.Add(i);
            }
            SelectList li = new SelectList(listPage, id);
            ViewBag.PageList = li;

            return View(list);
        }

    }
}

  添加视图--用默认的Index视图

@{
    Layout = null;
}
@using Mvc简单分页.Models;
@model List<Car>
<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>分页</title>
</head>
<body>
    <div>
        <h1>分页</h1>
        @using (@Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "form2" }))//******************
        {
            //string str = ViewBag.PAGESIZE;
            @:每页 @Html.TextBox("id", (int)ViewBag.PAGESIZE, new { onchange = "form.submit()" }) 条<input id="Button1" type="button" value="确定" />
        }

        <table border="0" width="100%" cellpadding="5" cellspacing="1" bgcolor="navy" style="text-align: center">
            <tr style="color: white; background-color: navy; text-align: center; font-weight: bold">
                <th>代号</th>
                <th>车型</th>
                <th>系列</th>
                <th>厂商</th>
                <th>价格</th>
            </tr>
            @{
                foreach (Car data in Model)
                {
                    <tr bgcolor="white">
                        <td>@data.Code</td>
                        <td>@data.Name</td>
                        <td>@data.Brand1.Brand_Name</td>
                        <td>@data.Brand1.Productor.Prod_Name</td>
                        <td>@data.Price</td>
                    </tr>
                }
            }
        </table>
        @*@{
                int nowPageNo = (int)ViewBag.PageNo;
                int nextPageNo = nowPageNo + 1;
                if (nowPageNo == new CarBF().GetPageCount(5))
                {
                    nextPageNo = nowPageNo;
                }
                int prevPageNo = nowPageNo - 1;
                if (nowPageNo == 1)
                {
                    prevPageNo = 1;
                }
            }*@
        @Html.ActionLink("首页", "Index", new { id = 1 })
        @Html.ActionLink("上一页", "Index", "Home", new { id = (int?)ViewBag.PrevPegeNo }, null)
        @Html.ActionLink("下一页", "Index", "Home", new { id = (int)ViewBag.NextPageNo }, null)
        @Html.ActionLink("尾页", "Index", new { id = (int)ViewBag.PageCount })
        <div style="display:inline-block">
            @using (Html.BeginForm("Index", "Home"))
            {
                @:转到:@Html.TextBox("id", null, new { size = 2 })<input type="submit" value="Go" />
             }
        </div>
        一共<font color="red"> @ViewBag.PageCount </font>页,当前是第<font style="color:red"> @ViewBag.PageNo </font>页
        <div style="display:inline-block">
            @using (Html.BeginForm("Index", "Home"))
            {
                @:转到:@Html.DropDownList("id", (SelectList)ViewBag.Pagelist)<input type="submit" value="Go" />
             }
        </div>
    </div>
</body>
</html>

  效果图

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
博文来源广泛,如原作者认为我侵犯知识产权,请尽快给我发邮件 664507902@qq.com联系,我将以第一时间删除相关内容。

posted @ 2015-09-12 18:56  木头园—OOIP  阅读(1288)  评论(0编辑  收藏  举报