MVC中使用MVCPager簡單分頁

來源:互聯網
上載者:User

標籤:cPage   style   blog   http   color   使用   

一、建立資料庫以及建立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/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.