zoukankan      html  css  js  c++  java
  • MVC 分页查询(汽车表)

    id改成1,代表着从第一页开始查询

    (一)controlles

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using MvcApplication1.Models;
    namespace MvcApplication1.Controllusing 
    {
        public class PagedCarController : Controller
        {
            private const int PAGESIZE = 3;
    
            public ActionResult Index(int id)//翻页
            {
                int pageCount =new CarBF().GetPageCount(PAGESIZE);
                ViewBag.PageNo = id;
                ViewBag.PageCount = pageCount;
                
                List<int> pagelist = new List<int>();
                for (int i = 1; i <= pageCount; i++)
                {
                    pagelist.Add(i);
                }
                SelectList selectList = new SelectList(pagelist,id);
    
                ViewBag.PageList = selectList;
    
                List<Car> list = new CarBF().Select(PAGESIZE, id);
                return View(list);
            }
    
        }
    }

    (二) models

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace MvcApplication1.Models
    {
        public class CarBF
        {
            private MyDBDataContext _Context = new MyDBDataContext();
            public List<Car> Select(int pageSize,int pageNo) //pageNo从1开始算的。
            {
                var query = _Context.Car.Skip(pageSize * (pageNo - 1)).Take(pageSize);
                return query.ToList();
            }
            public int GetPageCount(int pageSize)
            {
                int rowsCount = _Context.Car.Count();
                int pageCount = (int)Math.Ceiling( 1.0* rowsCount / pageSize);
                return pageCount;
            }
        }
    }

    (三 ) view

    @using MvcApplication1.Models;
    @model List<Car>
    @{
        Layout = null;
    }
    
    <!DOCTYPE html>
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Index</title>
        <script language="javascript">
            function dochange() {
                var s = document.getElementById("pageid").value;
                var str = "new {id="+s+"}";
                window.location.href = "/PagedCar/Index/" + s;
            }
        </script>
    </head>
    <body>
        <div>
            <ul>
                @foreach( Car data in Model ){
                <li>@data.Name</li>
                }
            </ul>
            @{
                int pageCount = (int)ViewBag.PageCount;
                int nowPage = (int)ViewBag.PageNo;
                int prevPage = nowPage - 1;
                int nextPage = nowPage + 1;
                }
            @Html.ActionLink("首页", "Index", "PagedCar", new { id=1},null)
            @Html.ActionLink("上一页", "Index", "PagedCar", new { id = prevPage }, new { onclick = (prevPage <= 0 ? "return false" : "return true") })
            @Html.ActionLink("下一页", "Index", "PagedCar", new { id = nextPage }, new { onclick=(nextPage > pageCount?"return false;":"return true;" )})
            @Html.ActionLink("尾页", "Index", "PagedCar", new { id = pageCount },null)
            转向 @Html.DropDownList("pageid", ViewBag.PageList as SelectList, new { onchange="dochange()"}) 页
        </div>
    </body>
    </html>

    ★ 最后一定要添加一个JS 的文件,否则无法正常运行。

    效果图:

  • 相关阅读:
    【简●解】[AHOI2009]中国象棋
    【讲●解】KMP算法
    【简●解】POJ 1185,LG P2704【炮兵阵地】
    学习网站整理
    【讲●解】火车进出栈类问题 & 卡特兰数应用
    洛谷4556 [Vani有约会]雨天的尾巴
    BZOJ2212或洛谷3521 [POI2011]ROT-Tree Rotations
    洛谷1119 灾后重建
    洛谷1462(重题1951) 通往奥格瑞玛的道路(收费站_NOI导刊2009提高(2))
    BZOJ2721或洛谷1445 [Violet]樱花
  • 原文地址:https://www.cnblogs.com/w-wz/p/4647443.html
Copyright © 2011-2022 走看看