.NET多数据过滤器

时间:2018-03-02 10:53:26

标签: c# .net entity-framework model-view-controller

我一直在使用EF6教程并创建了效果很好的过滤器!但是,该指南仅显示如何制作一个过滤器。虽然他们需要不同的字符串来操作,但我已经制作了多个过滤器。但是,分页使用searchString(其中一个过滤器字符串)根据过滤结果显示。如何在分页的同时操作多个过滤器?

过滤器和分页:

        if (searchString != null)
        {
            page = 1;
        }
        else
        {
            searchString = currentFilter;
        }

        ViewBag.CurrentFilter = searchString;

        var clients = from s in db.Clients.Include(c => c.MJTopics).Include(c => c.UserTable)
        select s;
        if (!String.IsNullOrEmpty(searchString))
        {
            clients = clients.Where(s => s.clientN.Contains(searchString)
                                   || s.homePage.Contains(searchString));
        }
        if (!String.IsNullOrEmpty(NameString))
        {
            clients = clients.Where(s => s.clientEmail.Contains(NameString)
                                   || s.contName.Contains(NameString));
        }
        if (!String.IsNullOrEmpty(RIString))
        {
            clients = clients.Where(s => s.clientN.Contains(RIString));
        }
        if (!String.IsNullOrEmpty(TopicString))
        {
            clients = clients.Where(s => s.MJTopics.topicalTF.Contains(TopicString));
        }

分页视图:

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

    @Html.PagedListPager(Model, page => Url.Action("Index",
             new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))
</div>

在视图中过滤:

    <div class="col-md-4">
        @using (Html.BeginForm())
        {
            <p class="filtertext">Filter Clients:</p>
            <p class="filterbox">@Html.TextBox("searchString")</p>
            <p class="filterbox2"><input type="submit" value="Search" class="btn btn-default btn-sm selector2" /> </p>
        }
    </div>
    <div class="col-md-4">
        @using (Html.BeginForm())
        {
            <p class="filtertext">Find by Name: </p>
            <p class="filterbox"> @Html.TextBox("NameString")</p>
            <p class="filterbox2"><input type="submit" value="Search" class="btn btn-default btn-sm selector2" /></p>
        }
    </div>

1 个答案:

答案 0 :(得分:0)

PagedList.Mvc项目于2013年被放弃。该网站警告:

  

重要提示:不再维护此软件包。请参阅ernado-x / X.PagedList以获取替代品。

虽然5年后,这些变化可能足够大,你不能只用另一个替换一个。

替换的Nuget包是X.PagedList.Mvc

Github repo上的示例显示它使用了IQueryable&lt; T&GT;直接,没有过滤字符串,例如:

var products = MyProductDataSource.FindAllProducts(); //returns IQueryable<Product> representing an unknown number of products. a thousand maybe?

var pageNumber = page ?? 1; // if no page was specified in the querystring, default to the first page (1)
var onePageOfProducts = products.ToPagedList(pageNumber, 25); // will only contain 25 products max because of the pageSize

ViewBag.OnePageOfProducts = onePageOfProducts;
return View();

<!-- output a paging control that lets the user navigation to the previous page, next page, etc -->
@Html.PagedListPager( (IPagedList)ViewBag.OnePageOfProducts, page => Url.Action("Index", new { page }) )