先上一张效果图
从网上找的一个第三方引用MvcPaging,对于我这个新手来说看懂实在太难。。。不过还是照着例子做了
首先是Controller部分:
using MvcPaging;//千万不要忘了引用 public class ProductController : AppController { // // GET: /Product/ Droplets.Models.DropletsEntities db; public ProductController() { db = new DropletsEntities(); } protected override void Dispose(bool disposing) { if (disposing) { db.Dispose(); } base.Dispose(disposing); } public ActionResult Index(int page=1) { int size = 8; var q = from t in db.WenZhang where t.LanMu2_Id == 1 && t.PictureCaptions != null orderby t.Id descending select t;//自己只需要从数据库中将需要的数据读出来 var pagedList = q.ToPagedList((page-1), size); //其他方面这位大神已经搞定,我们只需要调用就可以了 return View(pagedList);//对于我这新手,强类型和ViewBag都不会用,这个和之前相比,改为强类型传参 }
View部分 :(这里也需要改为强类型传参)
@using MvcPaging @using Droplets.Models @model IPagedList<WenZhang> @{ ViewBag.Title = "Products"; List<LanMu2> id = ViewBag.LanMu2Name; WenZhang neirong = ViewBag.ChanPinNeiRong; } <link href="~/Content/FenYeJsCss/Site.css" rel="stylesheet" /> <nav class="breadcrumb noindex"> <ul class="inner cf noindex"> <li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="/Home/Index" itemprop="url"><span itemprop="title">Droplets</span></a></li> <li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="/Product/Index" itemprop="url"><span itemprop="title">Products</span></a></li> </ul> </nav> <div class="content"> <section class="weiss"> <div class="inner cf"> <style type="text/css" media="screen"> .section-teaserblock-teaser_block_1 { width: 990px; } .section-teaserblock-teaser_block_1 .teaser { float: left; width: 228px; margin-right: 13px; } .section-teaserblock-teaser_block_1 .teaser img { width: 228px; } </style> <div class="section teaser-4column section-teaserblock-teaser_block_1 inner"> @if (Model != null) { foreach(WenZhang item in Model) //改为强类型传参,其实我自己没看懂。。 { if (item.PictureCaptions != null) { <div> <div class="box teaser withtopborder" style="width: 220px;height:221px;font-size:9px;"> <h3 class="tpt"> <a href="/Product/List?id=@item.Id" title="@item.Title" target=""> @(item.Title.Length > 20 ? item.Title.Substring(0, 20) + "..." : item.Title) </a> </h3> <a class="picturelink" href="/Product/List?id=@item.Id" title="@item.Title" target=""> <img alt="@item.Title" src="@item.PictureCaptions" style="width: 220px;height:152px"> </a> <div style="width:233px;height:1px;margin:0px auto;padding:0px;background-color:#D5D5D5;overflow:hidden;"></div> </div> </div> } } } </div> </div> </section> <div class="pager">//分页 @Html.Pager(Model.PageSize, Model.PageNumber, Model.TotalItemCount) </div> </div>
就这样了
MvcPaging下载:http://pan.baidu.com/s/1dENBSCL