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/)算 ...
随机推荐
- 最小生成树Prim算法
文章作者:甘航 文章来源:http://www.cnblogs.com/ganhang-acm/转载请注明,谢谢合作. 由于数据结构老师布置的一道题 ,我看prim算法看了半天还是一知半解. 在浏览 ...
- CodeForces - 27E--Number With The Given Amount Of Divisors(反素数)
CodeForces - 27E Number With The Given Amount Of Divisors Submit Status Description Given the number ...
- iOS中正确的截屏姿势
昨天写了个用到截屏功能的插件,结果问题不断,今天终于解决好了,把debug过程中所有尝试过的截屏方法都贴出来吧- 第一种 这是iOS 3时代开始就被使用的方法,它被废止于iOS 7.iOS的私有方法, ...
- as3声谱效果,有在线演示地址,能够播放本地音乐
来源:潮汕IT男 简单的as3声谱效果,能够播放本地音乐. tag=as3" style="word-wrap:break-word; margin:0px; padding:0p ...
- 倒计时IE10+
直接上代码,dome 里边有我做的列表倒计时(多个同时倒计时)下面是我做的例子,颜色可以自己设置的 <p name="daojishi" style="width: ...
- JS - 删除确认
<a href="javascript:if(confirm('确实要删除吗?'))location='<{:U('Admin/Update/deleteuserinfo', a ...
- ZOJ 3326 An Awful Problem 模拟
只有在 Month 和 Day 都为素数的时候才能得到糖 那就模拟一遍时间即可. //#pragma comment(linker, "/STACK:16777216") //fo ...
- django 时间计数
value必须replace(tzinfo=None)变成datetime格式,否则格式不对不能相减.
- ASP.NET MVC 5 学习教程:Details 和 Delete 方法详解
原文 ASP.NET MVC 5 学习教程:Details 和 Delete 方法详解 在教程的这一部分,我们将研究一下自动生成的 Details 和Delete 方法. Details 方法 打开M ...
- Flask web开发 处理Session
本文我们在上篇文章<Flask web开发 处理POST请求(登录案例)>的基础上,来讲述Flask对session的支持. 在上面案例上,我们需要修改和新增如下功能 1.登录成功后的 ...