我找不到使用 PagedList 进行过滤的方法

I can't find a way to filter using PagedList

好吧,我想我的问题其实很简单。 这是我的第二个 table...第一个加载大约需要 3 分钟。在寻找一种让它更快的方法时,我发现了这个:using a nuget。 但我不能让它过滤我的记录。 它确实过滤了一次并显示过滤的第一页,但是当我点击任何页面时它不再过滤。 在部分视图的底部(显示我的 table 的地方)我有过滤的辅助方法。在这里,我只是将页面单独发送到控制器...但是在视图(索引)中,我按品牌过滤它们,品牌也将品牌单独发送到控制器。

控制器

public ActionResult Index(string brand_name, int? page)
        {
            //returns IQueryable<Product> representing an unknown number of products. a thousand maybe?
            //var products = MyProductDataSource.FindAllProducts();
            ViewData["brand_name"] = brand_name;
            // if no page was specified in the querystring, default to the first page (1)
            var pageNumber = page.HasValue ? page.Value : 1;

            //ViewBag.OnePageOfProducts = onePageOfProducts;

            if (!string.IsNullOrWhiteSpace(brand_name))
            {
                // will only contain 12 products max because of the pageSize
                IPagedList<Material> onePageOfProducts = db.EContent_MaterialsFinalViewWithBcos
                    .Select(i => new Material
                    {
                        Brand = i.Brand,
                        Category = i.Category,
                        Language = i.Language,
                        Bco = i.Bco,
                        MaterialCod = i.MaterialCod,
                        Derivation = i.Derivation,
                        Artwork = i.Artwork,
                        BcoDelivery = i.BcoDelivery,
                        MaterialId = i.MaterialId
                    })
                    .Where(p => p.Brand.ToLower() == brand_name.ToLower())
                    .OrderBy(i => i.MaterialCod)
                    .ToPagedList<Material>(pageNumber, defaultPageSize);

                return View("Index", onePageOfProducts);
            }
            else
            {
                // will only contain 12 products max because of the pageSize
                IPagedList<Material> onePageOfProducts = db.EContent_MaterialsFinalViewWithBcos
                    .Select(i => new Material
                    {
                        Brand = i.Brand,
                        Category = i.Category,
                        Language = i.Language,
                        Bco = i.Bco,
                        MaterialCod = i.MaterialCod,
                        Derivation = i.Derivation,
                        Artwork = i.Artwork,
                        BcoDelivery = i.BcoDelivery,
                        MaterialId = i.MaterialId
                    }).OrderBy(i => i.MaterialCod).ToPagedList<Material>(pageNumber, defaultPageSize);

                return View("Index", onePageOfProducts);
            }
        }

查看

@using PagedList.Mvc <!--import this so we get our HTML Helper-->

@model IPagedList<eContentMVC.Models.Material>

<div style="padding-top: 5px"></div>
@using (Html.BeginForm("Index", "Materials", FormMethod.Get))
{
    <div class="col-lg-3">
        <div class="input-group">
            <span class="input-group-btn">
                <button class="btn btn-default" type="submit"><i class="glyphicon glyphicon-search"></i> Search</button>
            </span>
            <input class="span2" id="appendedInputButton" type="text" name="brand_name" placeholder="Search Brand for..." />                 
        </div><!-- /input-group -->
    </div><!-- /.col-lg-3 -->
    <div style="padding-top: 20px"></div>
    <div id="grid-list">
        @{ Html.RenderPartial("_AjaxMaterialList", Model); }
    </div>    
}

局部视图

@using PagedList.Mvc <!--import this so we get our HTML Helper-->

@model IPagedList<eContentMVC.Models.Material>

<table>Some table here</table>
<div class="panel panel-primary filterable">
    <div class="centerAlign">
        <!-- output a paging control that lets the user navigation to the previous page, next page, etc -->
        @Html.PagedListPager(Model, page => Url.Action("Index", "Materials", new
       {
           //It would be awesome if I could have some brand_name = brand
           page
       }))
    </div>
</div>

当您单击另一个页码时,您将在控制器中得到 brand_name => null 或 empty。我看到您将 brand_name 存储在 ViewData 中,因此您可以在视图中再次检索它,并在 TextBox 中显示它,因此它总是与页码一起发送到控制器。你可以这样做:

<input class="span2" id="appendedInputButton" type="text" name="brand_name" placeholder="Search Brand for..." value="@ViewData["brand_name"]"/>