参考网站  http://www.webdiyer.com/mvcpager/demos/

这个插件非常简单易用,如果想快速使用 可以参考我这篇文章,其实参考网站也是非常简单的

首先选择你的web项目安装插件,在nuget里面直接安装,我这里的版本是 3.0

在这里我提供的是一个带搜索的分页

我现在控制器里写一个Search的Action,代码如下:


 public ActionResult Search(int id = , string kword = null)
        {
            //查询数据库
            using (var db = new ShortTraining.Model.ShortTrainDbContext())
            {
                var query = db.ConsultationMsgModels.AsQueryable();
                if (!string.IsNullOrWhiteSpace(kword))
                    query = query.Where(m => m.StudentName.Contains(kword));
                //得到一个集合
                var model = query.OrderByDescending(a => a.Id)//查询得到一个Iqueryable
                    .Select(m => new Model.ViewModels.ConsultationMsg()//将Iqueryable poco成为我们的视图模型
                { Id = m.Id, StudentCollege = m.StudentCollege, StudentName = m.StudentName, StudentPhoneNo = m.StudentPhoneNo, StudentQQ = m.StudentQQ }).ToPagedList(id, );
                return View(model);
            }
        }

这里面进行了一个POCO


然后我们要针对这个搜索页面创建一个视图 代码如下:

 @using Webdiyer.WebControls.Mvc;
@model PagedList<ShortTraining.Model.ViewModels.ConsultationMsg>
@{
Layout = null;
} <!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
@using (Html.BeginForm("Search", ViewContext.RouteData.GetRequiredString("Controller"), new RouteValueDictionary { { "id", "" } }, FormMethod.Get))
{
<div class="well well-sm">
<span>搜索关键字:</span><input type="text" name="kword" value="@Request.QueryString["kword"]" style="width:120px" /><input type="submit" value="搜索(S)" accesskey="S" />
<span>(请输入“吴起”或“吴起县”进行测试)</span>
</div>
}
@Html.Partial("_ConsultationMsgTable", Model)
@Html.Pager(Model).Options(o => o.SetPageIndexParameterName("id").SetPagerItemTemplate("{0}&nbsp;"))
</div>
</body>
</html>

我在视图的第一行引用了  @using Webdiyer.WebControls.Mvc;  命名空间。

如果不使用这个命名空间,PagedList和@Html.Pager 都会报错的。

然后,我们要为这个视图创建一个Partal视图  _ConsultationMsgTable.cshtml 用来放置列表数据

代码如下:

 @using Webdiyer.WebControls.Mvc;
@model PagedList<ShortTraining.Model.ViewModels.ConsultationMsg>
<table class="table table-bordered table-striped">
<tr>
<th class="nowrap">序号</th>
<th>
@Html.DisplayNameFor(m=>m.StudentName)
</th>
<th>
@Html.DisplayNameFor(model => model.StudentPhoneNo)
</th>
<th>
@Html.DisplayNameFor(model => model.StudentQQ)
</th>
<th>
@Html.DisplayNameFor(model => model.StudentCollege)
</th>
</tr>
@{ int i = ;}
@foreach (var item in Model)
{
<tr>
<td>@(Model.StartItemIndex + i++)</td>
<td>
@Html.DisplayFor(modelItem => item.StudentName)
</td>
<td>
@Html.DisplayFor(modelItem => item.StudentPhoneNo)
</td>
<td>
@Html.DisplayFor(modelItem => item.StudentQQ)
</td>
<td>
@Html.DisplayFor(modelItem => item.StudentCollege)
</td>
</tr>
}
</table>

以上是核心,下面我列出我的两个模型,这两个模型一个是我的entity model,一个是我的view model 解释我的POCO:

entity model

view model:

显示效果如下:

在MVC项目中分页使用MvcPager插件的更多相关文章

  1. 转 mvc项目中,解决引用jquery文件后智能提示失效的办法

    mvc项目中,解决用Url.Content方法引用jquery文件后智能提示失效的办法   这个标题不知道要怎么写才好, 但是希望文章的内容对大家有帮助. 场景如下: 我们在用开发开发程序的时候,经常 ...

  2. 谈谈MVC项目中的缓存功能设计的相关问题

    本文收集一些关于项目中为什么需要使用缓存功能,以及怎么使用等,在实际开发中对缓存的设计的考虑 为什么需要讨论缓存呢? 缓存是一个中大型系统所必须考虑的问题.为了避免每次请求都去访问后台的资源(例如数据 ...

  3. 在 ASP.NET MVC 项目中使用 WebForm、 HTML

    原文地址:http://www.cnblogs.com/snowdream/archive/2009/04/17/winforms-in-mvc.html ASP.NET MVC和WebForm各有各 ...

  4. MVC项目中如何判断用户是在用什么设备进行访问

    使用UAParser在C#MVC项目中如何判断用户是在用什么设备进行访问(手机,平板还是普通的电脑) 现在我们开发的很多web应用都要支持手机等移动设备.为了让手机用户能有更加好的用户体验,我们经常为 ...

  5. 在已有的Asp.net MVC项目中引入Taurus.MVC

    Taurus.MVC是一个优秀的框架,如果要应用到已有的Asp.net MVC项目中,需要修改一下. 1.前提约定: 走Taurus.MVC必须指定后缀.如.api 2.原项目修改如下: web.co ...

  6. ASP.NET MVC项目中App_Code目录在程序应用

    学习ASP.NET MVC,如果你是开发ASP.NET MVC项目的,也许你去为项目添加前ASP.NET项目的APP_Code目录,在这里创建与添加的Class类,也许你无法在MVC项目所引用. 那这 ...

  7. 如何在mvc项目中使用apiController

    文章地址:How do you route from an MVC project to an MVC ApiController in another project? 文章地址:How to Us ...

  8. 在ASP.NET MVC项目中使用极验验证(geetest)

    时间 2016-03-02 18:22:37 smallerpig 原文  http://www.smallerpig.com/979.html 主题 ASP.NET MVC   geetest开发体 ...

  9. MVC项目中的分页实现例子

    在开发项目中,写了一个分页实现的例子,现在把源代码贴上,以供以后写代码时参考 在列表的头部,有如下显示, 这个代表一个页面显示10条记录,总共22条记录. 这个是页面底部的显示 那么如何来显示这个分页 ...

随机推荐

  1. thinkphp5项目--企业单车网站(九)(加强复习啊)(花了那么多时间写的博客,不复习太浪费了)

    thinkphp5项目--企业单车网站(九)(加强复习啊)(花了那么多时间写的博客,不复习太浪费了) 项目地址 fry404006308/BicycleEnterpriseWebsite: Bicyc ...

  2. 【record】9.24..10.1

    因为参加比赛所以做得比较少了

  3. IOS开发核心动画六:动画组

    #import "ViewController.h" @interface ViewController () @property (weak, nonatomic) IBOutl ...

  4. php实现找链表中环的入口节点(画图、看评论)

    php实现找链表中环的入口节点(画图.看评论) 一.总结 画图.看评论 二.php实现找链表中环的入口节点 题目描述: 一个链表中包含环,请找出该链表的环的入口结点. 三.代码 第一步,找环中相汇点. ...

  5. windows 空闲超时 非管理员如何破解

    windows 空闲超时 非管理员如何破解

  6. INotifyPropertyChanged接口的详细说明

    在windows phone开发8.1:数据绑定中,我们了解了数据绑定的基本知识.今后几篇文章会继续深入了解数据绑定.今天我们来看在数据绑定中十分重要的INotifyPropertyChanged接口 ...

  7. 解析C#内存管理

    C#内存管理解析 前言:对于很多的C#程序员来说,经常会很少去关注其内存的释放,他们认为C#带有强大的垃圾回收机制,所有不愿意去考虑这方面的事情,其实不尽然,很多时候我们都需要考虑C#内存的管理问题, ...

  8. [SCSS] Organize SCSS into Multiple Files with Partials

    Tired of dealing with monolithic CSS files? Are requests for multiple CSS files hurting your perform ...

  9. [Angular] Angular CLI

    Create an app with routing config: ng new mynewapp --routing If you want to generate a new module wi ...

  10. window对象属性alert、confirm、prompt怎么使用?

    window对象属性alert.confirm.prompt怎么使用? 一.总结 1.参数依次复杂,返回值依次复杂,但是感觉都是一一继承的,所以很好想也很好写. 二.window对象alert.con ...