MVC中使用MVCPager简单分页

一、建立数据库以及建立MVC项目

自己随便建立一个数据库,并且添加数据。我建立的数据库如下。

二、建立LINQ to SQL映射。

然后一步步点确定

三、编写代码

在Controllers中建立控制器,模板为空。建立的项目如下:

要添加视图Index

在Controller中选中Index 并且添加视图, 模板选中List, 如下

注意后台要添加对MvcPager的程序集引用,后台代码如下:

using Webdiyer.WebControls.Mvc;

 public class StudentController : Controller
    {
        //
        // GET: /Student/

        public ActionResult Index(int id=1)
        {
           Models.StudentEntityDataContext student= new Models.StudentEntityDataContext();
           IQueryable<Models.S> p = from c in student.S
                                    select c;
           PagedList<Models.S> m = p.ToPagedList(id,10);
            return View(m);
        }
    }

  

前台代码如下:

@model PagedList<MvcApplication1.Models.S>

@{
    Layout = null;
}
@using Webdiyer.WebControls.Mvc;  //添加对程序集的引用
<!DOCTYPE html>

<html>
<head>
    <title>Index</title>
// 控制分页的CSS样式
    <style type="text/css">
        table, td
        {
            font: 100% Arial, Helvetica, sans-serif;
        }
        table
        {
            width: 100%;
            border-collapse: collapse;
            margin: 1em 0;
        }
        th, td
        {
            text-align: left;
            padding: .5em;
            border: 1px solid #fff;
        }
        th
        {
            background: #328aa4;
            color: #fff;
        }
        td
        {
            background: #e5f1f4;
        }

        /*div布局*/
        .div float
        {
            float: left;
            width: 50%;
        }
        #div pages
        {
            height: 300px;
        }
        /*分页工具栏样式*/
        .pages
        {
            color: #045FB4;
            font-weight: bold;
            font-size: 14px;
        }

        .pages .item
        {
            padding: 1px 6px;
            font-size: 14px;
        }
        /*号码页数*/

        .pages .cpb
        {
            color: #045FB4;
            padding: 1px 6px;
            font-size: 13px;
        }
        /*当前页数*/

        .pages a
        {
            text-decoration: none;
            padding: 0 5px;
            border: 1px solid #BDBDBD;
            margin: 0 2px;
            color: #000;
            font-weight: normal;
        }

        .pages a:hover
        {
            background-color: #0174DF;
            color: #fff;
            border: 1px solid #0174DF;
            text-decoration: none;
            font-weight: normal;
        }
        </style>
</head>
<body>
    <p>
        @Html.ActionLink("Create New", "Create")
    </p>
    <table>
        <tr>
            <th>
                学号
            </th>
            <th>
                姓名
            </th>
            <th>
                 性别
            </th>
            <th>
                准考证
            </th>
            <th>
                专业
            </th>
            <th>
                院系
            </th>
            <th>
                考场
            </th>
            <th>
                座位
            </th>
            <th>
                编排校区
            </th>
            <th>
                语言级别
            </th>
            <th>
                年级
            </th>
            <th></th>
        </tr>

    @foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.学号)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.姓名)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.性别)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.准考证)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.专业)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.院系)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.考场)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.座位)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.编排校区)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.语言级别)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.年级)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
                @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
                @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
            </td>
        </tr>
    }

    </table>

//这里是分页的控制
  <div>
 @Html.AjaxPager(Model, new PagerOptions() { CssClass = "pages", PageIndexParameterName = "id",
    ShowPageIndexBox = true, PageIndexBoxType = PageIndexBoxType.TextBox, ShowGoButton = true,
    PageIndexBoxWrapperFormatString="转到{0}"}, new AjaxOptions() { UpdateTargetId = "dvOrders" })

    </div>

</body>
</html>

  四、最终分页效果

代码下载地址: http://download.csdn.net/detail/luoyangwyb/7581651

MvcPager官方下载网址:http://mvcpager.codeplex.com/releases/view/47201

Demo的示例网址:http://en.webdiyer.com/

MVC中使用MVCPager简单分页,布布扣,bubuko.com

时间: 2024-08-28 01:19:09

MVC中使用MVCPager简单分页的相关文章

ASP.NET MVC中使用MvcPager异步分页+在分页中复选框下一页上一页也保持选中

ASP.NET MVC 分页使用的是作者杨涛的MvcPager分页控件  地址:http://www.webdiyer.com/mvcpager/demos/ajaxpaging/ 这个分页控件在里面有很好的的案例,及注意事项 分页在我们的项目中是经常需要使用到的,普通分页体验是在是太差了,每一次点击下一步,会造成页面刷新,自己都看不过去了 ,O(∩_∩)O哈哈~ 所以这次我们要使用这个控件在做一个MvcPager的异步分页,分页的时候我们只刷新表格,而不是刷新页面 下面我们开始吧 一.分页 首

在 asp.net mvc中的简单分页算法

//第一步:建立如下分页实体类:namespace MVCPager.Helpers { /// <summary> /// 简单分页算法类 /// </summary> public class Pager { public int RecordCount { get; set; } public int PageIndex { get; set; } public int PageSize { get; set; } public int PageCount { get { r

mvc中利用Attribute特性来进行进行简单的登陆验证

前段时间一直比较忙.好不容易忙完.闲的没事干,就捣腾了下mvc(ef),因为以前都是用三层框架来进行开发,mvc用的也不是很多...众所周知,在三层里面我们一般都是建一个基类,然后在基类里面写验证登录方法,然后在需要验证登录的页面继承这个基类即可...但到了mvc里面所有的视图页面的操作都转移到了控制器了..这个时候我们在按照三层的方式建一个基类来继承验证登录,就没办法走通了...今天我就给大家来展示一个利用Attribute特性来验证登录.如果还有不知道这个东东的,可以百度一下Attribut

MVC中分页的实现

我在格斗人网 (www.helpqy.com) 中使用了下面的分页技术. 分页可以采用troygoode提供的开源包,其开源网站主页为:https://github.com/TroyGoode/PagedList.具体使用方法如下所示: 1. 通过NuGet下载PagedList.Mvc包,这个包会自动下载另外一个包PagedList,如下所示: 2. 在controller中引入以上两个包,如下所示: 3. 在controller中的ActionResult函数中按照如下最小结构进行调用: 1

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

在ASP.NET Core MVC中构建简单 Web Api

Getting Started 在 ASP.NET Core MVC 框架中,ASP.NET 团队为我们提供了一整套的用于构建一个 Web 中的各种部分所需的套件,那么有些时候我们只需要做一个简单的 Web Api 程序怎么办呢? 在 GitHub 中的 ASP.NET Core MVC 源码里面,我们只要关注 Microsoft.AspNetCore.Mvc 这个包,那么除了这个包之外它还包含这些: Microsoft.AspNetCore.Mvc.ApiExplorer Microsoft.

mvc中html导出成word下载-简单粗暴方式

由于工作需求,需要把html简历页导出成word下载.网上搜索了很多解决方案,基本都是用一些插件,然后写法也很麻烦,需要创建模板什么的. 固定替换值  代码一大堆.但是对于我的需求来说  并没有什么用,因为我这边的数据不能固定.  所以只能另寻办法,皇天不负有心人   找了一天 终于找到了  哈哈 mvc中的FlieResult  非常强大  可以直接传入html   指定文件格式  直接返回Flie文件下载 不多说了   直接上代码吧   非常之简单粗暴 后台代码: [ValidateInpu

在MVC中实现和网站不同服务器的批量文件下载以及NOPI下载数据到Excel的简单学习

嘿嘿,我来啦,最近忙啦几天,使用MVC把应该实现的一些功能实现了,说起来做项目,实属感觉蛮好的,即可以学习新的东西,又可以增加自己之前知道的知识的巩固,不得不说是双丰收啊,其实这周来就开始面对下载在挣扎啦,不知道从哪下手,而且自己针对一个文件下载的小小练习还是写过的,但是和项目中的下载完全就是两个世界,所以我只能抱着学习的心情查找资料啦,刚开始由于leader没有说怎么个下载的办法,我只能自己看些有关下载的资料啦,周一只是在猜测的学习,然后通过询问各路大神.就新学习了NOPI,当我看到Nopi下

SpringSecurity 在MVC 中的简单使用(翻译的,稍加改动)

Spring Security允许开发人员轻松地将安全功能集成到J2EE Web应用程序中,它通过Servlet过滤器实现“用户自定义”安全检查. 在本教程中,我们将向您展示如何在Spring MVC中集成Spring Security 3.0并安全访问.在集成成功后,当我们查看页面的内容时用户需要先输入正确的“用户名”和“密码”. 1.目录结构 项目最终目录如下所示: 2.Spring Security依赖关系 为了正常运行 Spring security , 你需要加入 “spring-se