[RESOLVED]How to get automatic next page in cshtml ?

Hi all,

Can I edit to get automatic next page in the following cshtml code : ( Eg: Previouse 2 3 4 5 Next )

@{
var db = Database.Open("SmallBakery"); 
var selectQueryString = "SELECT * FROM Product ORDER BY Name"; 
}

<html> 
<body> 
<h1>Small Bakery Products</h1> 
<table> 
<tr>
<th>Id</th> 
<th>Product</th> 
<th>Description</th> 
<th>Price</th> 
</tr>
@foreach(var row in db.Query(selectQueryString))
{

<tr> 
<td>@row.Id</td> 
<td>@row.Name</td> 
<td>@row.Description</td> 
<td style="text-align:right">@row.Price</td> 
</tr> 
}
</table> 
</body> 
</html>

Help me please ? Thanks..

You can add your own paging. I cover how to do that in this article: http://www.mikesdotnetting.com/article/150/web-pages-efficient-paging-without-the-webgrid

Hello,

You can use WebGrid Helper:

@{
var db = Database.Open("SmallBakery"); 
var selectQueryString = "SELECT * FROM Product ORDER BY Name"; 
var grid = new WebGrid(source: selectQueryString, rowsPerPage: 10);
}

<html> 
<body>
   @grid.GetHtml()
</body> 
</html>

rowsPerPage specifies how many items will be displayed on each page.

You can find more information on the following page:

http://www.asp.net/web-pages/overview/getting-started/introducing-aspnet-web-pages-2/displaying-data

Good luck..

Hello,

what is your result?

Did you try the solutions?

Hi Mike, how would you go about limiting the number of page links shown. I can see you having a large number of them if there is a lot of data?

Something like this could make the work:

    <a href="/Paging/1">First</a>
    @{
        var startingPage = 1;
        if (totalPages >= 5) 
        {
            if((page - 2) > 0 && (page + 2) <= totalPages)
            {
                startingPage = page - 2;
            } else if ((page + 2) > totalPages)
            {
                startingPage = totalPages - 4;
            }
        }
        for (var i = startingPage; i <= startingPage + 4 && i <= totalPages; i++){
            <a href="/Paging/@i">@i</a>
        }
    }
    <a href="/Paging/@totalPages">Last</a>

Maybe the code could be optimized …

Hi ayanmesut,

Thanks for your help. This way I already learns from you but I want such as
Mekis
article example… Thank you.

Leave a Reply