paging in asp.net mvc

后端 未结 4 1631
忘掉有多难
忘掉有多难 2021-01-23 03:11

i have an asp.net website where i do paging through on the code behind using:

    PagedDataSource objPds = new PagedDataSource
                                 {         


        
4条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-23 04:01

    I will explain the way to implement pagination in asp.net mvc.

    ProductController.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 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());
    }
    

    ProductModel.cs

    public class Product
        {
            private ProductContext db = new ProductContext ();
    
            public int GetTotalProducts()
            {
                return db.Products.Count();
            }
    
            public List GetProducts()
            {
                return db.Products.ToList();
            }
            public List GetProducts(int start, int offset)
            {
                IEnumerable query = from m in db.Products
                                           orderby m.Id descending
                                           select m;
                query = query.Skip(start).Take(offset);
                return query.ToList();
            }
    
        }
    

    Index.aspx

    
        

    View Users

    <%: Html.ActionLink("Create New", "Create") %>

    Total Users: <%= ViewData["totalProducts"] %>

    <% if ((int)ViewData["totalProducts"] == 0) { %>

    Sorry! No Users Found.

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

    <% if ((string)ViewData["pageLinks"] != "") { %> <%= ViewData["pageLinks"] %>

    <% } %> <% } %>

提交回复
热议问题