• 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 的文件,否则无法正常运行。

    效果图:

  • 相关阅读:
    【前端优化之拆分CSS】前端三剑客的分分合合
    ipad&mobile通用webapp框架前哨战
    如何判断一个DOM元素正在动画,一个CSS“阻塞”JS的例子
    关于前端框架升级与全站样式替换的简单建议
    【HTML5&CSS3进阶04】CSS3动画应该如何在webapp中运用
    【HTML5&CSS3进阶03】Jser与Csser如何一起愉快的翻新老组件
    【HTML5&CSS3进阶学习02】Header的实现·CSS中的布局
    【HTML5&CSS3进阶学习01】气泡组件的实现
    【模块化编程】理解requireJS-实现一个简单的模块加载器
    【小贴士】探一探javascript中的replace
  • 原文地址:https://www.cnblogs.com/w-wz/p/4647443.html
Copyright © 2020-2023  润新知