zoukankan      html  css  js  c++  java
  • 基于IPagedList 的 Asp.Net MVC 分页

    去年写过一篇MVC分页的

    自己用的一个ASP.Net MVC分页拿出来分享下

    现在发一个改进版

    里面用到的IPagedList我自己也记不清是从哪里COPY来的了。呵呵

    这里我就不把IPagedList的代码贴出来了,要使的下载DEMO自己拿吧。

    image

    后台对数据的分页

    public ActionResult Index([DefaultValue(1)]int p,[DefaultValue(10)]int pagesize)
            {
                var model = Database.List.OrderByDescending(z=>z.Id).ToPagedList(p-1,pagesize);
                return View("Index",model);
            }
    

    个人最近比较喜欢使用DefaultValue

    当然也可以写成

    public ActionResult Index(int? p,int? pagesize)
            {
                p = p ?? 1;
                pagesize = pagesize ?? 10;
                var model = Database.List.OrderByDescending(z=>z.Id).ToPagedList(p-1,pagesize);
                return View("Index",model);
            }
    

    这里ToPagedList 这个扩展方法里第一个参数是index

    image

    懒得去改成page了,所以就用了 p-1

    前台中调用

    <%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" 
    Inherits="System.Web.Mvc.ViewPage<MvcDemo.IPagedList<MyModel>>" %>
    
        <%= Html.PagerBar(ViewData.Model) %>
        <table>
            <thead>
            <tr>
                <th>Id</th>
                <th>姓?名?</th>
                <th>邮ê箱?</th>
                <th>个?人?主÷页3</th>
                <th>生ú日?</th>
                <th>&nbsp;</th>
                </tr>
            </thead>
            <tbody>
            <%foreach (var item in ViewData.Model)
                {%>
                <tr>
                    <td><%=item.Id%></td>
                    <td><%=item.Name%></td>
                    <td><%=item.EMail%></td>
                    <td><%=item.Url%></td>
                    <td><%=item.Birthday.ToShortDateString()%></td>
                    <td><%=Html.ActionLink("编辑", "Edit", new {id = item.Id},
    new {@class = "d", width = 600})%></td>
                </tr>
            <%}%>
            </tbody>
        </table>
    

    样式啥的控制和上一篇差不多我不在这里说了

    DEMO中的效果图:

    image

    image

     image

    image

    DEMO下载:/Files/francis67/MvcDemo.rar

  • 相关阅读:
    字段名删不掉
    刷新f5/ctrl+f5
    大量数据模拟
    sub_query join drupal7 view_query_alter
    测试风格的代码
    csv/excel乱码
    window.location.reload(true)的异步现象
    扫描条形码
    yield %%% generator
    batch example
  • 原文地址:https://www.cnblogs.com/francis67/p/1732569.html
Copyright © 2011-2022 走看看