【问题标题】:ASP.NET MVC. PagedList filtering by multiple paramsASP.NET MVC。 PagedList 多参数过滤
【发布时间】:2015-12-08 21:22:23
【问题描述】:

我有一个带有过滤器参数的网页,并创建了简单的类来通过 Where() 方法处理该参数。

但是通过 Html.Action 传递参数存在一个问题。

搜索选项:

public class Document
{
    public string Name { get; set; }
    public string Title { get; set; }
    public string Param1{ get; set; }
    public string Param2{ get; set; }
}

控制器动作

 public ActionResult Index(int? page, Document filter)
    {
        ViewBag.Params= documentFilter;
        IEnumerable<Document> DocumentList = dbContext.Documents.ToList();
        DocumentList = DocumentList.CustomFilter(documentFilter);
        var pageNumber = page ?? 1;
        var onePageOfProducts = DocumentList.ToPagedList(pageNumber, 50);
        ViewBag.Documents = onePageOfProducts;
        return View();
    }

过滤效果很好,但是如果使用分页控制,params会丢失。

分页助手:

@Html.PagedListPager((IPagedList)ViewBag.Documents, 
page => Url.Action("Index", new { page , filter = (Document)ViewBag.filter}))
//Also tried Model instead of ViewBag.filter

我无法将参数传递给动作控制。

有没有办法做到这一点?

【问题讨论】:

  • Model 不起作用,因为您没有将模型传递给视图,您只是在使用ViewBag。老实说,出于学习的目的,只是假装ViewBag 根本不存在。你使用它的方式会起作用,但它很草率。

标签: c# asp.net entity-framework filtering paging


【解决方案1】:

您需要使用 ajax 调用而不是 helper。编写ajax调用并将参数作为post方法传递。

【讨论】:

    【解决方案2】:

    看起来 ViewBag.filter 从未设置,因此您的分页将无法正常工作。

    这是一个很棒的教程,可以详细回答您的问题: http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-21
      • 1970-01-01
      • 2022-01-13
      • 1970-01-01
      • 2011-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多