2016-07-06 76 views
1

請你讓我知道是否有任何方式來做ASP.NET MVC中的分頁,而不使用任何外部nuget包(例如:PageList.mvc)。其實我正在尋找一個服務器端解決方案來做MVC本身的分頁。分頁ASP.NET MVC中沒有使用任何外部nuget包

如果有人知道答案,請幫助我。

感謝 Nishad

+0

請注意,模型 - 視圖控制器標籤是有關該模式的問題。 ASP.NET-MVC實現有一個特定的標籤。 –

回答

1

您可以使用WebGrid。它不是NuGet軟件包,它是System.Web.Helpers命名空間的一部分,並提供開箱即用的分頁功能。

1.型號:

public class Product 
{ 
    public int ID { get; set; } 
    public string Description { get; set; } 
} 

2.View:

@model IEnumerable<MVCTutorial.Models.Product> 

@{ 
    Layout = null; 
    WebGrid grid = new WebGrid(Model, canPage: true, canSort: false, rowsPerPage: 2); 
} 

@grid.GetHtml(
tableStyle: "table", 
columns: grid.Columns(
    grid.Column("ID", "ID", format: @<text> @item.ID 
    </text>, style: "p13"), 
    grid.Column("Description","Description", format: @<text> @item.Description</text>))) 

3.Controller:

public class HomeController : Controller 
{ 
    public ActionResult GetProducts() 
    { 
     var p1 = new Product { ID = 1, Description = "Product 1" }; 
     var p2 = new Product { ID = 2, Description = "Product 2" }; 
     var p3 = new Product { ID = 3, Description = "Product 3" }; 
     var p4 = new Product { ID = 4, Description = "Product 4" }; 

     var products = new List<Product> { p1, p2, p3, p4 }; 

     return View(products); 
    } 
} 
+0

謝謝丹尼斯。這對我很有幫助。這是我發現的東西。再次感謝。 – user3796885

+0

嗨丹尼斯,你可以讓我知道如何將參數作爲ActionResult GetProducts(布爾值)傳遞給action action方法,當我點擊每個分頁時。 – user3796885