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

时间:2015-07-01 21:08:13

标签: asp.net-mvc view pagedlist

好吧,我认为我的问题实际上非常简单。 这是我的第二张表...第一张表需要3分钟才能加载。为了找到让它更快的方法,我发现了这个:using a nuget。 但我不能让它过滤我的记录。 它会过滤一次并显示第一页过滤,但是当我点击任何页面时它不再过滤。 在局部视图的底部(显示我的表格)我有过滤器的辅助方法。在这里,我只是将页面单独发送到控制器......但在视图(索引)中,我按品牌过滤它们,品牌也将品牌单独发送给控制器。

控制器

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>

1 个答案:

答案 0 :(得分:0)

当您点击其他页码时,您将获得brand_name =&gt;在控制器中为null或为空。我看到你将brand_name存储在ViewData中,因此你可以在视图中再次检索它,并在TextBox中显示它,因此它总是与页码一起发送到控制器。你可以这样做:

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