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/)算 ...
随机推荐
- ceph源码之一
转自于:http://blog.csdn.net/changtao381/article/details/8698935 一.概述: 其结构如下:在src 里, 网络通信: msg 里面 包括了网 ...
- 【转】CentOS 6 服务器安全配置指南
原文连接: CentOS 6 服务器安全配置指南(通用) Linux 是一个开放式系统,可以在网络上找到许多现成的程序和工具,这既方便了用户,也方便了黑客,因为他们也能很容易地找到程序和工具来潜入 L ...
- QGraphicsTextItem中的文字对齐
QGraphicsTextItem类可以放到QGraphicsScene或者QGraphicsItem上,用来显示格式化的文本内容,如HTML,当然纯文本也可以显示.如果只是显示纯文本,可以使用QGr ...
- Chapter 7 代理模式
代理模式:为其它对象提供一种代理以控制对这个对象的访问. 代码: package xiao; class Girl{ private String name; public void setName( ...
- Qt学习:线程间共享数据(使用信号槽传递数据,必须提前使用qRegisterMetaType来注册参数的类型)
Qt线程间共享数据主要有两种方式: 使用共享内存.即使用一个两个线程都能够共享的变量(如全局变量),这样两个线程都能够访问和修改该变量,从而达到共享数据的目的: 使用singal/slot机制,把数据 ...
- android 根据域名得到IP
public static String GetInetAddress(String host) { String IPAddress = ""; InetAddress Retu ...
- 设计模式(八)装饰器模式Decorator(结构型)
设计模式(八)装饰器模式Decorator(结构型) 1. 概述 若你从事过面向对象开发,实现给一个类或对象增加行为,使用继承机制,这是所有面向对象语言的一个基本特性.如果已经存在的一个类缺少某些方法 ...
- 基于visual Studio2013解决C语言竞赛题之0419误差控制
题目 解决代码及点评 /************************************************************************/ /* 19 ...
- C++将文件内容一次性读入内存
结合字符串流,将文件中的内容一次性读入内存,代码如下: #include <string> using std::ostringstream; using std::ifstream; u ...
- WebView使用详解
一.用WebView处理页面导航 可以加载本地资源也可以加载web资源,区别是我们要加载本地资源要实现在assets文件夹里添加一个.html资源.调取网页的时候可以用loadUrl方法把网址添加进去 ...