0

I have a pagination problem. I have a Product model, it has a string ProductCategory attribute. One page can take 4 products, whenever it exceeds 4, it points out page 2. The problem is that when I click "Car" category and click page 2, it takes every product, rather than taking only "Car" .

I got the book from the book, ASP.NET MVC 4, published by apress.

Here is my ProductListViewModel:

    public class ProductsListViewModel
{
    public List<Product> Products { get; set; }
    public PagingInfo PagingInfo { get; set; }
    public string CurrentCategory { get; set; }
}

Here is my ProductController's List Action: When I debug the application, at the click at page 2, category parameter is null.

        public ViewResult List(string category, int page = 1)
    {
        ProductsListViewModel model = new ProductsListViewModel
        {
            Products = repository.Products
            .Where(p => category == null || p.ProductCategory.Equals(category))
            .OrderBy(p => p.ProductID)
            .Skip((page - 1) * PageSize).Take(PageSize).ToList(),                
            PagingInfo = new PagingInfo
            {
                CurrentPage = page,
                ItemsPerPage = PageSize,
                TotalItems = category == null ?
                repository.Products.Count() :
                repository.Products.Where(e => e.ProductCategory == category).Count()
            }
        };

        model.CurrentCategory = category;

        return View(model);
    }

Here is my List View:

@model SportsStore.WebUI.Models.ProductsListViewModel
@{
       ViewBag.Title = "Products";
 }
 @foreach (var p in Model.Products)
 {
       <div class="item">
       @Html.Partial("ProductSummary", p)
       </div>
 }

  <div class="pager">
            @Html.PageLinks(Model.PagingInfo, x => Url.Action("List", new { page = x,        ProductCategory = Model.CurrentCategory }))

ProductSummary is a partial view that views the product. Pagelinks is a extention methods:

        public static MvcHtmlString PageLinks(this HtmlHelper html, PagingInfo pagingInfo,
    Func<int, string> pageUrl)
    {
        StringBuilder result = new StringBuilder();
        for (int i = 1; i <= pagingInfo.TotalPages; i++)
        {
            TagBuilder tag = new TagBuilder("a"); // Construct an <a> tag
            tag.MergeAttribute("href", pageUrl(i));
            tag.InnerHtml = i.ToString();
            if (i == pagingInfo.CurrentPage)
                tag.AddCssClass("selected");
            result.Append(tag.ToString());
        }
        return MvcHtmlString.Create(result.ToString());
    }

enter image description here

As pictured above, when I click page 2, it gets every product, rather than car. How can I solve it? Thanks in advance.

NOTE: Routes has been added below:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {

        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapRoute(null,
        "",
        new
        {
            controller = "Product",
            action = "List",
            category = (string)null,
            page = 1
        }
        );

        /*
         *http://localhost:56701/?page=2 olmasındansa http://localhost:56701/Page4 olmasını sağlayan kod parçacığı.
         *
         */
        routes.MapRoute(null,
        "Page{page}",
        new { controller = "Product", action = "List", category = (string)null },
        new { page = @"\d+" }
        );
        routes.MapRoute(null,
        "{category}",
        new { controller = "Product", action = "List", page = 1 }
        );
        routes.MapRoute(null,
        "{category}/Page{page}",
        new { controller = "Product", action = "List" },
        new { page = @"\d+" }
        );
        routes.MapRoute(null, "{controller}/{action}");
    }
}

NOTE 2: Here is the result when I click page 2 of the Car category: enter image description here

As you see below, at the page 2, every car items exist. (Blue rectangle). But I do no want to see page 3 and 4 at the bottom of the page (Red rectangle). Thanks in advance.

6
  • 1
    Can you please provide the routes that you setting up, looking at the cod you've provided that might be the most likely place to start looking for the issue Commented Dec 30, 2015 at 13:45
  • Dear Paul Hadfield, I have updated the post. Commented Dec 30, 2015 at 13:49
  • 1
    Can you please add a screen shot showing the URL when you click on page two for cars product. Also can you confirm that when you click on the categories it shows the correct list, it only goes wrong when viewing a category and clicking to see page 2+ Commented Dec 30, 2015 at 17:14
  • Yes I confirm. I am away of my computer, I only can post screenshot tomorrow. Commented Dec 30, 2015 at 18:37
  • When I click car, localhost:56701/Car has appeared on my computer, however, all pages has been shown. (I expect page 1 and page 2 at the buttom of the page, however, page 3 and 4 also exists.) When I click the page 2 of the Car Category, localhost:56701/Car/Page2 appears at the address bar, only cars appears at the page, however, page 3 and 4 also exist at the bottom. Commented Dec 31, 2015 at 7:35

1 Answer 1

1

The solution has implemented in the book, and it seems it has been escaped from my attention. Two revisions should be made in the source code, one in List.cshtml :

<div class="pager">
@Html.PageLinks(Model.PagingInfo, x => Url.Action("List", new { page = x, category = Model.CurrentCategory }))

Another is in the List action of the ProductController:

        public ViewResult List(string category, int page = 1)
    {
        ProductsListViewModel viewModel = new ProductsListViewModel
        {
            Products = repository.Products
            .Where(p => category == null || p.ProductCategory == category)
            .OrderBy(p => p.ProductID)
            .Skip((page - 1) * PageSize)
            .Take(PageSize).ToList(),                
            PagingInfo = new PagingInfo
            {
                CurrentPage = page,
                ItemsPerPage = PageSize,
                **TotalItems = category == null ?
                    repository.Products.Count() :
                    repository.Products.Where(e => e.ProductCategory == category).Count()**
            },
            CurrentCategory = category
        };
        return View(viewModel);
    }

TotalItems attribute shall be updated as mentioned above.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm glad you managed to get it sorted, sorry I didn't get back to you, was busy with the break over new year's eve. Just as some advice you might want to spend a little more time on your initial question if you're asking another one, your answer appears to answer a slightly different issue than the initial question that you asked, but I can see how you're answering the issue you clarified in your second update. Enjoy learning ASP.NET MVC; it is definitely worth it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.