Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Next and Previous buttons not displayed when there are more than 4 posts #262

Open
wpqs opened this issue Sep 21, 2020 · 0 comments
Open

Comments

@wpqs
Copy link

wpqs commented Sep 21, 2020

When you have more the 4 posts the home page should show a summary of the latest four and display a previous button so that the user can navigate to the earlier posts (assuming of course that your appsettings.json has blog:postsPerPage=4). Unfortunately, the previous button doesn't appear.

There seems to be a bug in views\blog\index.cshtml

@{
    int currentPage = int.Parse(ViewContext.RouteData.Values[Constants.page] as string ?? "0");

    int pageCount;
    pageCount = int.TryParse(ViewData[Constants.TotalPostCount].ToString(), out pageCount) ? 0 : pageCount / 2;
}

When int.TryParse() succeeds pageCount is zero, and when it fails pageCount is zero/2. Therefore I propose the following fix:

  1. Add to Constants.cs
public static readonly string PostsPerPage = "PostsPerPage";
  1. Update BlogController.Index() and BlogController.Category()
this.ViewData[Constants.PostsPerPage] = this.settings.Value.PostsPerPage;
  1. Change Index.cshtml
    int currentPage = int.Parse(ViewContext.RouteData.Values[Constants.page] as string ?? "0");

	int postsPerPage;
	postsPerPage = (int.TryParse(ViewData[Constants.PostsPerPage].ToString(), out postsPerPage) && (postsPerPage > 0)) ? postsPerPage : 4;
    int pageCount;
    pageCount = int.TryParse(ViewData[Constants.TotalPostCount].ToString(), out pageCount) ? pageCount / postsPerPage : 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant