对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>

  效果图

MVC简单分页的更多相关文章

  1. ASP.NET MVC 简单分页代码

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

  2. Asp.net MVC 简单分页 自做简单分页

    Asp.net MVC 简单分页:   public static string Pager(int page,int pageSize,int total)         {           ...

  3. MVC简单分页(未实现无刷新分页)

    分页Html辅助方法 using System.Text; using System.Web: using System.Web.Mvc; namespace System.Web.Mvc { pub ...

  4. asp.net mvc简单分页实例

    @{ ViewBag.Title = "Index"; } @{ int pageIndex = (int)ViewBag.CurrentPage; int pageCount = ...

  5. Mvc 简单分页代码

    ) { string userid = EndUserLoginManage.Instance.loginUserID; ICommentInfoBLL c_bll = new CommentInfo ...

  6. MVC001之mvcpager简单分页

    描述:用mvcpager实现简单分页功能 参考网址: http://www.cnblogs.com/iamlilinfeng/archive/2013/03/11/2951460.html http: ...

  7. 关于Mvc的分页写法

    关于asp.net mvc的分页,网上已经有很多了.本来也想借用,先看了杨涛写的分页控件,感觉用起来稍微有点复杂,而我只需要简单的分页.分页我写过很多次,原理也熟悉,就是构造首页.上一页.下一页及末页 ...

  8. JavaScript简单分页,兼容IE6,~3KB

    简介 兼容IE6+及现代浏览器的简单分页,支持同一页面多个分页. 使用 Browser <link rel="stylesheet" href="css/GB-pa ...

  9. 基于存储过程的MVC开源分页控件--LYB.NET.SPPager

    摘要 现在基于ASP.NET MVC的分页控件我想大家都不陌生了,百度一下一大箩筐.其中有不少精品,陕北吴旗娃杨涛大哥做的分页控件MVCPager(http://www.webdiyer.com/)算 ...

随机推荐

  1. Python开源异步并发框架

    Python开源异步并发框架的未来 2014年3月30日,由全球最大的中文IT社区CSDN主办的“开源技术大会·” (Open Source Technology Conference ,简称OSTC ...

  2. 用C语言写一个程序,得出当前系统的整形数字长(16位,32位,64位)等,不能使用sizeof()

    #include <iostream>#include <cmath>using namespace std; int main(){ int num = -1; unsign ...

  3. 首届全球RTB(实时竞价)广告DSP算法大赛

    首届全球RTB(实时竞价)广告DSP算法大赛 竞赛指南     RTB (Real Time Bidding, 实时竞价) 是近年来计算广告领域最激动人心的进展之一. 它增加了展示广告的透明度与效率, ...

  4. CUDA samples 第三章 sample reference 概况

    示例代码分为下列几类: 1.   Simple Reference 基础CUDA示例,适用于初学者, 反应了运用CUDA和CUDA runtime APIs的一些基本概念. 2.   Utilitie ...

  5. (step4.2.1) hdu 1372(Knight Moves——BFS)

    解题思路:BFS 1)马的跳跃方向 在国际象棋的棋盘上,一匹马共有8个可能的跳跃方向,如图1所示,按顺时针分别记为1~8,设置一组坐标增量来描述这8个方向: 2)基本过程 设当前点(i,j),方向k, ...

  6. sizeof,终极无惑(上)

    0. 前向声明 sizeof,一个其貌不扬的家伙,引无数菜鸟竟折腰,小虾我当初也没少犯迷糊,秉着“辛苦我一个,幸福千万人”的伟大思想,我决定将其尽可能具体的总结一下. 但当我总结的时候才发现,这个问题 ...

  7. android -- 蓝牙 bluetooth (三)搜索蓝牙

    接上篇打开蓝牙继续,来一起看下蓝牙搜索的流程,触发蓝牙搜索的条件形式上有两种,一是在蓝牙设置界面开启蓝牙会直接开始搜索,另一个是先打开蓝牙开关在进入蓝牙设置界面也会触发搜索,也可能还有其它触发方式,但 ...

  8. hdu4513之manacher算法

    吉哥系列故事——完美队形II Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) T ...

  9. 将 Excel 数据导入 MySql

    能承受上万数据量,速度快,并且使用了事务,不会出现某条数据错误而导致部分数据插入(要是全部成功要是一条都不成功,测试过程中没出现失败),需要的朋友可以参考下 1.NPOI 2.MySql.Data 这 ...

  10. POJ 3528 求三维凸包表面积

    也是用模板直接套的题目诶 //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include < ...