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>
效果图

MVC简单分页的更多相关文章
- ASP.NET MVC 简单分页代码
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- Asp.net MVC 简单分页 自做简单分页
Asp.net MVC 简单分页: public static string Pager(int page,int pageSize,int total) { ...
- MVC简单分页(未实现无刷新分页)
分页Html辅助方法 using System.Text; using System.Web: using System.Web.Mvc; namespace System.Web.Mvc { pub ...
- asp.net mvc简单分页实例
@{ ViewBag.Title = "Index"; } @{ int pageIndex = (int)ViewBag.CurrentPage; int pageCount = ...
- Mvc 简单分页代码
) { string userid = EndUserLoginManage.Instance.loginUserID; ICommentInfoBLL c_bll = new CommentInfo ...
- MVC001之mvcpager简单分页
描述:用mvcpager实现简单分页功能 参考网址: http://www.cnblogs.com/iamlilinfeng/archive/2013/03/11/2951460.html http: ...
- 关于Mvc的分页写法
关于asp.net mvc的分页,网上已经有很多了.本来也想借用,先看了杨涛写的分页控件,感觉用起来稍微有点复杂,而我只需要简单的分页.分页我写过很多次,原理也熟悉,就是构造首页.上一页.下一页及末页 ...
- JavaScript简单分页,兼容IE6,~3KB
简介 兼容IE6+及现代浏览器的简单分页,支持同一页面多个分页. 使用 Browser <link rel="stylesheet" href="css/GB-pa ...
- 基于存储过程的MVC开源分页控件--LYB.NET.SPPager
摘要 现在基于ASP.NET MVC的分页控件我想大家都不陌生了,百度一下一大箩筐.其中有不少精品,陕北吴旗娃杨涛大哥做的分页控件MVCPager(http://www.webdiyer.com/)算 ...
随机推荐
- elk 索引
zjtest7-redis:/usr/local/logstash-2.3.4/config# cat logstash_agent.conf input { file { type => &q ...
- wchar_t*和char*之间的互相转换的那些事
最近在看一写PE文件格式的东西,想做一个读取PE文件信息的小工具,中间遇到将LPVOID格式无法转换到LPTSTR格式,强制转换屡试屡败,多显示乱码.我们知道LPVOID格式可以直接转换到char * ...
- for语句之打印三角形问题
1.左下角直角三角形 Console.Write("请输入要打印几行:"); int a = Convert.ToInt32(Console.ReadLine()); ; i &l ...
- java生成压缩图
链接地址:http://blog.sina.com.cn/s/blog_407a68fc0100nrba.html package util; import java.awt.image.Buffer ...
- [转]如何从MySQL官方Yum仓库安装MySQL5.6
参考博文: 如何从MySQL官方Yum仓库安装MySQL5.6 Centos 升级Mysql版本或者Yum安装Mysql5.6 2013年10月,MySQL开发团队正式宣布支持Yum仓库,这就意味着我 ...
- POJ1985 DFS【STL__vector_的应用】
vector 向量 相当于一个数组 在内存中分配一块连续的内存空间进行存储.支持不指定vector大小的存储.STL内部实现时,首先分配一个非常大的内存空间预备进行存储,即capacit ...
- 如何删除openfire for苹果,彻底卸载!
早前在MAC上安装了一个名为openfire的服务插件,用这个进行一些插件安装和设置,使ichat能登录MSN帐号.但现在最新的MSN已经有所改善,支持视频与语音,离线信息接收.而openfire有个 ...
- json_response的用法
传统的方法是当我们处理一个表单时,我们Post数据给服务器,服务器对数据进行处理后将数据返回给用户,此时部分写法是用页面刷新的方式将页面重新刷新一次呈现给用户,这样的话用户相当于读入了两次页面,人一多 ...
- 测试css2.1选择器IE6/7/8支持情况,属性选择器/子元素/相邻
环境 xp ie 8 , ietester <!doctype html> <html> <head> <meta charset="utf-8&q ...
- docker学习笔记:修改无法启动的容器中的内容
我们可能会碰到这样的一个问题,在容器执行过程中,修改了容器的内容(如配置文件信息),但因为修改出了问题.导致容器关闭后,无法启动. 这事需要重新修改配置文件. 正常情况下可以通过 docker exe ...