当我过滤产品时,PagedList.Mvc分页丢失了

时间:2015-09-06 01:12:38

标签: c# asp.net asp.net-mvc asp.net-mvc-5 pagedlist

我使用的是PagedList.Mvc,当我使用Filter Like时 localhost:2241 /三星,转到第2页 浏览器重定向到localhost:2241 /而不是 本地主机:2241 /三星/第2页

我如何解决这个问题

ProductController.cs

public class ProductController : Controller
{
    private readonly IProductRepository repository;  
    public ProductController(IProductRepository repo)
    {
        repository = repo;
    }
    public ViewResult List(int? page, string category)
    {
        ProductListViewModel model = new ProductListViewModel
        {
            Prodcuts = repository.Products
                .Where(p => category == null || p.Category == category),
            CurrentCategory = category
        };
        return View (model.Prodcuts.ToList().ToPagedList(page ?? 1, 3));
    }
}

List.cshtml

@model IPagedList<OnlineShoppingStore.Domain.Entities.Product>
@using PagedList;
@using PagedList.Mvc;
....
@foreach (var p in Model)
{
    @Html.Partial("ProductSummary",p)
}
@Html.PagedListPager(Model, page => Url.Action("List", new {page }))

ProductListViewModel.cs

public class ProductListViewModel
{
    public IEnumerable <Product> Prodcuts { get; set; } 
    public string CurrentCategory { get; set; }
}

我该怎么办?

1 个答案:

答案 0 :(得分:-1)

<强> ProductController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OnlineShoppingStore.Domain.Abstract;
using PagedList;
using PagedList.Mvc;
using OnlineShoppingStore.WebUI.Models;



namespace OnlineShoppingStore.WebUI.Controllers
{
public class ProductController : Controller
{
    private readonly IProductRepository repository;

    public ProductController(IProductRepository repo)
    {
        repository = repo;
    }
    public ViewResult List(int? page, string category, string Filter_Value)
    {

        if (category != null)
        {
            page = 1;
        }
        else
        {
            category = Filter_Value;
        }

        ViewBag.FilterValue = category;

        ProductListViewModel model = new ProductListViewModel
        {
            Prodcuts = repository.Products
                .Where(p => category == null || p.Category == category),
            CurrentCategory = category

        };


                 return View (model.Prodcuts.ToList().ToPagedList(page ?? 1, 3));

           }


       }
 }

<强> ProductListViewModel.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using OnlineShoppingStore.Domain.Entities;

namespace OnlineShoppingStore.WebUI.Models
{
public class ProductListViewModel
{
    public  IEnumerable <Product> Prodcuts { get; set; } 
    public  string CurrentCategory { get; set; }

}
}

<强> List.cshtml

@model IPagedList<OnlineShoppingStore.Domain.Entities.Product>
@using PagedList;
@using PagedList.Mvc;


@{
ViewBag.Title = "Products";
}

@foreach (var p in Model)
{
@Html.Partial("ProductSummary",p)

}

@{

}
<div>

@Html.PagedListPager(Model, page => Url.Action("List", new {page,      Filter_Value = ViewBag.FilterValue }))


Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount