PagedList and Colums VB.Net

Peter Newman 66 Reputation points
2023-04-16T21:22:07.7133333+00:00

Trying to get PagedlList and search working on my MVC project (vb.net) I've cut down the code ( index fields) , but I'm struggling to add the pageList to the Index.vbhtml Looking for a simple walkthrough that I can apply to my code and include column sorting and Search, as I need to apply this to other pages

Controller
Function Index(ByVal page As Integer?) As ActionResult
            Dim _context As New DynamicsCRMContext
            Dim Tsets = From tsetlst In _context.TransactionSets Where (tsetlst.new_licenceid_id = _selectedLicenceID) Order By tsetlst.new_serialnumber Descending
            Dim PageSize As Integer = 20
            Dim PageNumber As Integer = (If(page, 1))
            Return View(Tsets.ToPagedList(PageNumber, PageSize))
        End Function

Index.vbhtml
@ModelType IEnumerable(Of CBPortal.TransactionSets)

@Code
    ViewData("Title") = "Transaction Sets"
End Code


<Table Class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(Function(model) model.new_serialnumber)
        </th>

        <th></th>
    </tr>
    @For Each item In Model
        @<tr>
            <td>
                @Html.DisplayFor(Function(modelItem) item.new_serialnumber)
            </td>

        </tr>
    Next
</Table>

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,470 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Lan Huang-MSFT 29,251 Reputation points Microsoft Vendor
    2023-04-17T05:56:39.7766667+00:00

    Hi @Peter Newman,
    Because I don't know your model and data, I wrote an example of using PagedList in VB.Net MVC View with test data.

    @ModelType PagedList.IPagedList(Of WebVBDemo.QuestionViewModel)
    
    @Imports PagedList.Mvc
    
    @Code
        ViewData("Title") = "Transaction Sets"
    End Code
    
    <Table Class="table">
    
        @For Each item In Model
            @<tr>
                <td>
                    @Html.DisplayFor(Function(modelItem) item.QuestionName)
                </td>
    
            </tr>
        Next
    </Table>
    Page @(If(Model.PageCount < Model.PageNumber, 0, Model.PageNumber)) of @Model.PageCount
    
    @Html.PagedListPager(Model, Function(page) Url.Action("Contact", New With {page}))
    
    Function Contact(ByVal page As Integer?) As ActionResult
            Dim questions = {
                New QuestionViewModel With {
                .QuestionId = 1,
                .QuestionName = "Question 1"
            }, New QuestionViewModel With {
            .QuestionId = 1,
            .QuestionName = "Question 2"
            }, New QuestionViewModel With {
            .QuestionId = 1,
            .QuestionName = "Question 3"
            }, New QuestionViewModel With {
            .QuestionId = 1,
            .QuestionName = "Question 4"
            }}
            Dim PageSize As Integer = 2
            Dim PageNumber As Integer = (If(page, 1))
            Return View(questions.ToPagedList(PageNumber, PageSize))
        End Function
    
    Public Class QuestionViewModel
        Public Property QuestionId As Integer
        Public Property QuestionName As String
    End Class
    

    Best regards,30

    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.