对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. SQL常用语句集合(不断更新)

    1.多条件 查询 上下级 所有数据 select * from OrgUnit where (ParentId = '3' or OrgId='3' or ParentId in (select Or ...

  2. .NET C#操作文件系统数据的常用方法总结 part1

    目录导航 1.0开启文件隐藏属性 1.1开启文件夹只读属性 1.2输出子文件夹路径 1.3统计文件数目 1.4批量重命名文件 1.5分割文件 1.6拼接文件 1.7读取用逗号分割的值 1.8读写压缩数 ...

  3. iOS 常用开源代码整理

    本文章不定期整理. 1.AFNetworking AFNetworking 采用 NSURLConnection + NSOperation, 主要方便与服务端 API 进行数据交换, 操作简单, 功 ...

  4. Ural 1297 Palindrome 【最长回文子串】

    最长回文子串 相关资料: 1.暴力法 2.动态规划 3.中心扩展 4.Manacher法 http://blog.csdn.net/ywhorizen/article/details/6629268 ...

  5. 【集训笔记】计算几何【HDOJ2036【HDOJ1086【HDOJ1115【HDOJ1147【HDOJ1392 【ZOJ2976

    改革春风吹满地 Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission(s): Accepted ...

  6. 如何在.Net中使用Redis

    Redis是一个key-value存储系统.和Memcached类似,但是解决了断电后数据完全丢失的情况,而且她支持更多无化的value类型,除了和string外,还支持lists(链表).sets( ...

  7. 金融管理 - MBA智库百科

    金融管理 - MBA智库百科     金融管理    出自 MBA智库百科(http://wiki.mbalib.com/)     金融管理(Financial Management)    目录  ...

  8. js 中实现sleep函数

    除了Narrative JS,jwacs(Javascript With Advanced Continuation Support)也致力于通过扩展JavaScript语法来避免编写让人头痛的异步调 ...

  9. libgdx, mouse 关节

    鼠标与body的交互就靠这个mouse 关节了. 在使用中:主要分成3步: 步1:mouseDown : 这个时期,调用world->QueryAABB.它有一个回调接口,并依据鼠标指针指定一个 ...

  10. erlang 初体验

    近期測试了一下 erlang的坑... 如不出意外.... 大家第一眼看到这语法... 心里第一句一定是"我擦.这TM都是啥!!!!!" 没有变量!!! 没有结构体!!! 没有循环 ...