Programming Tutorials

Pagination in ASP.net core application

By: Linda Ng in Asp.net Tutorials on 2023-06-01  

To display a read-only grid of records with pagination in an ASP.NET Core view, you can follow these steps:

  1. Create a model class to represent your records. For example, let's assume you have a Record class with properties like Id, Name, Description, and Date.

  2. In your controller, retrieve the records from your data source and pass them to the view using a view model. The view model should include the records and pagination information. For example:

    public ActionResult Index(int page = 1, int pageSize = 10)
    {
        // Retrieve the records from your data source
        var records = GetRecordsFromDataSource();
    
        // Apply pagination
        var paginatedRecords = records.Skip((page - 1) * pageSize).Take(pageSize).ToList();
    
        // Calculate total pages
        var totalPages = (int)Math.Ceiling((double)records.Count / pageSize);
    
        // Create the view model
        var viewModel = new RecordViewModel
        {
            Records = paginatedRecords,
            PageNumber = page,
            PageSize = pageSize,
            TotalPages = totalPages
        };
    
        return View(viewModel);
    }
    
  3. Create a view model class to represent the data passed to the view. For example:
    public class RecordViewModel
    {
    public List<Record> Records { get; set; }
    public int PageNumber { get; set; }
    public int PageSize { get; set; }
    public int TotalPages { get; set; }
    }
  4. In your view (e.g., Index.cshtml), iterate over the records and display them in a grid format. You can use a table or any other HTML markup that suits your needs. Additionally, you can add navigation links for pagination. Here's an example:
    @model RecordViewModel
    <table>
    <thead>
    <tr>
    <th>Id</th>
    <th>Name</th>
    <th>Description</th>
    <th>Date</th>
    </tr>
    </thead>
    <tbody>
    @foreach (var record in Model.Records)
    {
    <tr>
    <td>@record.Id</td>
    <td>@record.Name</td>
    <td>@record.Description</td>
    <td>@record.Date</td>
    </tr>
    }
    </tbody>
    </table>
    <div>
    @if (Model.TotalPages > 1)
    {
    for (int i = 1; i <= Model.TotalPages; i++)
    {
    <a href="@Url.Action("Index", new { page = i, pageSize = Model.PageSize })">@i</a>
    }
    }
    </div>

This example demonstrates a basic implementation of a read-only grid with pagination. You may need to customize it based on your specific requirements and styling preferences.






Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in Asp.net )

Pagination in ASP.net core application

Severity Code Description Project File Line Suppression State Error CS0246 The type or namespace name 'JToken' could not be found.

Getting values from appsettings.json ASP.NET

Microsoft.Identity vs Microsoft.IdentityModel.Clients.ActiveDirectory

Severity Code Description Project File Line Suppression State Warning Found conflicts between different versions of the same dependent assembly.

Severity Code Description Project File Line Suppression State Error CS0308 The non-generic type 'List' cannot be used with type arguments.

Severity Code Description Project File Line Suppression State Error CS1061 'string[]' does not contain a definition for 'Any' and no accessible extension method 'Any' accepting a first argument of type 'string[]' could be found (are you missing a using directive or an assembly reference?)

One client credential type required either: ClientSecret, Certificate, ClientAssertion or AppTokenProvider must be defined when creating a Confidential Client. Only specify one

Things to note when changing a function to async in your controller

Passing a model globally to all Views in your Asp.net webapp

Development Mode in IIS for Asp.net projects

Severity Code Description Project File Line Suppression State Error CS0103 The name 'JsonConvert' does not exist in the current context.

IsPostBack in ASP.net

AmbiguousMatchException: The request matched multiple endpoints.

HttpError is not found in Asp,net core project

Latest Articles (in Asp.net)

Things to note when changing a function to async in your controller

AmbiguousMatchException: The request matched multiple endpoints.

Call an Action in a controller when user clicks a button in View

Button that is only clickable when the checkbox is checked

Pass the same model to multiple views within the same controller

Severity Code Description Project File Line Suppression State Error CS0103 The name 'Encoding' does not exist in the current context

Severity Code Description Project File Line Suppression State Error CS0103 The name 'JsonConvert' does not exist in the current context.

Passing a model globally to all Views in your Asp.net webapp

Severity Code Description Project File Line Suppression State Error CS0246 The type or namespace name 'JToken' could not be found.

Severity Code Description Project File Line Suppression State Error CS0308 The non-generic type 'List' cannot be used with type arguments.

One client credential type required either: ClientSecret, Certificate, ClientAssertion or AppTokenProvider must be defined when creating a Confidential Client. Only specify one

Severity Code Description Project File Line Suppression State Warning Found conflicts between different versions of the same dependent assembly.

Severity Code Description Project File Line Suppression State Error CS1061 'string[]' does not contain a definition for 'Any' and no accessible extension method 'Any' accepting a first argument of type 'string[]' could be found (are you missing a using directive or an assembly reference?)

Pagination in ASP.net core application

Microsoft.Identity vs Microsoft.IdentityModel.Clients.ActiveDirectory

Related Tutorials

Things to note when changing a function to async in your controller

AmbiguousMatchException: The request matched multiple endpoints.

Call an Action in a controller when user clicks a button in View

Button that is only clickable when the checkbox is checked

Pass the same model to multiple views within the same controller

Severity Code Description Project File Line Suppression State Error CS0103 The name 'Encoding' does not exist in the current context

Severity Code Description Project File Line Suppression State Error CS0103 The name 'JsonConvert' does not exist in the current context.

Passing a model globally to all Views in your Asp.net webapp

Severity Code Description Project File Line Suppression State Error CS0246 The type or namespace name 'JToken' could not be found.

Severity Code Description Project File Line Suppression State Error CS0308 The non-generic type 'List' cannot be used with type arguments.

One client credential type required either: ClientSecret, Certificate, ClientAssertion or AppTokenProvider must be defined when creating a Confidential Client. Only specify one

Severity Code Description Project File Line Suppression State Warning Found conflicts between different versions of the same dependent assembly.

Severity Code Description Project File Line Suppression State Error CS1061 'string[]' does not contain a definition for 'Any' and no accessible extension method 'Any' accepting a first argument of type 'string[]' could be found (are you missing a using directive or an assembly reference?)

Pagination in ASP.net core application

Microsoft.Identity vs Microsoft.IdentityModel.Clients.ActiveDirectory