2009-08-10 8 views
2

i ont un site asp.net où je ne feuilletant sur le code derrière l'utilisation:échange dans asp.net mvc

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

quelle est la meilleure façon équivalente de faire radiomessagerie pour asp.net-mvc. Je pense que cela devrait appartenir au code de la vue.

Répondre

11

Je voudrais juste définir un itinéraire personnalisé avec le numéro de la page en elle:

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

vous donnera ce genre d'URL:

http://localhost/books/4/ 

Ensuite, dans votre contrôleur d'action vous obtenez ce numéro de page :

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

     return View(); 
    } 
} 

Votre vue ne sera donc pas réellement au courant de la page en cours. Il affichera simplement une liste des enregistrements fournis.

Vous aurez également besoin de générer des liens vers différentes pages soit directement dans cette vue, soit peut-être dans votre page maître.

5

Il est un bon exemple de classe d'appel dans le projet 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); 
      } 
     } 
0

Si vous achetez:
Professional ASP.NET MVC 1.0 (Wrox Programmer to Programmer)

La section là au sujet Ajax et JsonResult ... très bonne description de la configuration d'une solution javascript et non-javascript. Je ne l'ai pas réellement implémenté, donc je ne m'en souviens pas trop, je me souviens juste quand je l'ai lu, je pensais que ça marcherait parfaitement pour paginer sur mon nouveau site.

tutoriel décent ici aussi:
http://www.asp.net/learn/mvc/tutorial-32-cs.aspx

0

Je vais expliquer la façon de mettre en œuvre dans asp.net mvc la pagination.

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

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

    } 

index.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>