Home > Enterprise >  Not able to get the next and prev button value in .NET Core using asp-route-data
Not able to get the next and prev button value in .NET Core using asp-route-data

Time:06-27

This is the sample code for .cshtml:

<li>
    <a asp-route-currentPage="@(Model.CurrentPage -1)"
        aria-label="Next">
       <span aria-hidden="true"><</span>
       <span >Previous</span>
    </a>
</li>

 <li>
     <a asp-route-currentPage="@Model.TotalResults[0]"
         aria-label="Last">
        <span aria-hidden="true">>|</span>
        <span >Last</span>
     </a>
 </li>

enter image description here

When I click next or previous button it's not adding or minus the current page value.

Is there is any way to solve it? Please let me know. Thanks in advance

public int CurrentPage { get; set; } = 1;
public int Count { get; set; }
public int PageSize { get; set; } = 5;

public int TotalPages => (int)Math.Ceiling(decimal.Divide(Count, PageSize));

public bool EnablePrevious => CurrentPage > 1;
public bool EnableNext => CurrentPage < TotalPages;

public dynamic TotalResults;

public void OnGet(int CurrentPage)
{
    if (CurrentPage == 0)
    {
        CurrentPage = 1;         
    }

    ViewData["Title"] = "Transactions List";
    Transactions = GetTransactions(CurrentPage);       
}

CodePudding user response:

By the looks of your OnGet method, you are not updating the CurrentPage value with the new one.

public void OnGet(int CurrentPage)
{
    ...
    this.CurrentPage = CurrentPage; // Added this
    ... 
}
  • Related