2

我有一个 asp.net 网站,我在其中使用以下代码进行分页:

    PagedDataSource objPds = new PagedDataSource
                                 {
                                     DataSource = ds.Tables[0].DefaultView,
                                     AllowPaging = true,
                                     PageSize = 12
                                 };

为 asp.net-mvc 进行分页的等效最佳方法是什么。我认为这实际上属于视图代码。

4

4 回答 4

11

我只想定义一个带有页码的自定义路由:

routes.MapRoute(
                "Books", // Route name
                "books/{page}", // URL with parameters
                new {controller = "Books", action = "List", page = 1}
                );

会给你这种Url:

http://localhost/books/4/

然后在您的控制器操作中,您将获得此页码:

public BooksController
{
    public ActionResult List (int page)
    {
        /* Retrieve records for the requested page from the database */

        return View ();
    }
}

因此,您的视图实际上不会知道当前页面。它只会显示提供的记录列表。

您还需要直接在此视图中或在您的母版页中生成指向各个页面的链接。

于 2009-08-10T11:41:49.533 回答
5

Nerd Dinner项目中有一个很好的分页类示例:

public class PaginatedList<T> : List<T> {

        public int PageIndex  { get; private set; }
        public int PageSize   { get; private set; }
        public int TotalCount { get; private set; }
        public int TotalPages { get; private set; }

        public PaginatedList(IQueryable<T> source, int pageIndex, int pageSize) {
            PageIndex = pageIndex;
            PageSize = pageSize;
            TotalCount = source.Count();
            TotalPages = (int) Math.Ceiling(TotalCount / (double)PageSize);

            this.AddRange(source.Skip(PageIndex * PageSize).Take(PageSize));
        }

        public bool HasPreviousPage {
            get {
                return (PageIndex > 0);
            }
        }

        public bool HasNextPage {
            get {
                return (PageIndex+1 < TotalPages);
            }
        }
于 2009-08-10T12:03:43.733 回答
0

如果购买:
专业 ASP.NET MVC 1.0(Wrox Programmer to Programmer)

那里关于 Ajax 和 JsonResult 的部分......关于如何设置 javascript 和非 javascript 解决方案的非常好的演练。我还没有真正实现它,所以我不太记得它,我只记得我读它的时候,我认为它可以完美地用于在我的新网站上进行分页。

这里也有不错的教程:
http ://www.asp.net/learn/mvc/tutorial-32-cs.aspx

于 2009-08-10T12:19:14.543 回答
0

我将解释在asp.net mvc中实现分页的方式。

产品控制器.cs

private ProductContext db = new ProductContext ();

public ActionResult Index()
{
    string pageString = "";
    try
    {
        pageString = Request.Url.Segments[3];
    }
    catch (Exception)
    {
        pageString = null;
    }

    int page = (String.IsNullOrEmpty(pageString)) ? 1 : Int32.Parse(pageString);
    Product userModel = new Product();
    int totalProducts = userModel.GetTotalProducts();

    PaginationFunction pagination = new PaginationFunction(true);
    pagination.BaseUrl = "/Product/Index/";
    pagination.TotalRows = totalProducts;
    pagination.CurPage = page;
    pagination.PerPage = 5;
    pagination.PrevLink = "Prev";
    pagination.NextLink = "Next";
    string pageLinks = pagination.GetPageLinks();
    int start = (page - 1) * pagination.PerPage;
    int offset = pagination.PerPage;

    List<Product> products = userModel.GetProducts(start, offset);

    ViewData["title"] = "Pagination in Asp.Net Mvc";
    ViewData["totalProducts"] = totalProducts;
    ViewData["products"] = products;
    ViewData["pageLinks"] = pageLinks;

    return View(db.Products.ToList());
}

产品模型.cs

public class Product
    {
        private ProductContext db = new ProductContext ();

        public int GetTotalProducts()
        {
            return db.Products.Count();
        }

        public List<Product> GetProducts()
        {
            return db.Products.ToList();
        }
        public List<Product> GetProducts(int start, int offset)
        {
            IEnumerable<Product> query = from m in db.Products
                                       orderby m.Id descending
                                       select m;
            query = query.Skip(start).Take(offset);
            return query.ToList();
        }

    }

索引.aspx

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <h2>View Users</h2>
    <p>
    <%: Html.ActionLink("Create New", "Create") %>
    </p>
    <p>Total Users: <strong><%= ViewData["totalProducts"] %></strong></p>

    <% if ((int)ViewData["totalProducts"] == 0)
       { %>
        <p>Sorry! No Users Found.</p>
    <% }
       else
       { %>    
    <table border="0" cellspacing="0" cellpadding="0" width="100%" class="list">
        <tr>
            <th>Name</th>
            <th>Price</th>
            <th>CreatedDate</th>
            <th>UpdatedDate</th>
            <th></th>
        </tr>

        <% foreach (Product u in (List<Product>)ViewData["products"]) 
           { %>        
            <tr>
                <td><%= u.Name%></td>
                <td><%= u.Price %></td>
                <td><%= u.CreatedDate %></td>
                <td><%= u.UpdatedDate%></td>
                <td>
                    <%: Html.ActionLink("Edit", "Edit", new { id=u.Id }) %> |
                    <%: Html.ActionLink("Details", "Details", new { id=u.Id }) %> |
                    <%: Html.ActionLink("Delete", "Delete", new { id=u.Id }) %>
                </td>
            </tr> 
        <% } %>
    </table>

        <br />
        <% if ((string)ViewData["pageLinks"] != "")
           { %>
           <%= ViewData["pageLinks"] %>
           <br /><br />
        <% } %>       
    <% } %>
</asp:Content>
于 2015-11-21T05:11:54.720 回答