asp.net mvc分页错误

时间:2015-12-30 13:39:49

标签: asp.net-mvc pagination

我有分页问题。我有一个Product模型,它有一个字符串 ProductCategory 属性。一个页面可以带4个产品,每当它超过4时,它指出第2页。问题是当我点击" Car"类别和点击第2页,它需要每个产品,而不是只采取"汽车"

我从apress发表的ASP.NET MVC 4这本书中得到了这本书。

这是我的ProductListViewModel:

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

这是我的ProductController列表操作:当我调试应用程序时,在第2页的点击中,category参数为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);
    }

这是我的列表视图:

@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是查看产品的局部视图。 Pagelinks是一种扩展方法:

        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

如上图所示,当我点击第2页时,它会获得每件产品,而不是汽车。我该如何解决? 提前致谢。

注意:下面添加了路线:

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}");
    }
}

注2: 当我点击 Car 类别的第2页时,结果如下: enter image description here

如下所示,在第2页,每个汽车项目都存在。 (蓝色矩形)。但我不想在页面底部看到第3页和第4页(红色矩形)。 提前谢谢。

1 个答案:

答案 0 :(得分:1)

该解决方案已经在书中实现,似乎已经从我的注意力中逃脱了。 应在源代码中进行两次修订,一次在List.cshtml中:

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

另一个是 ProductController 的List动作:

        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属性应如上所述进行更新。