Skip to content

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

Description

@wpqs

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;

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions