How to I apply filter while paginating in Asp.net MVC and entity Framework?

前端 未结 3 785
傲寒
傲寒 2020-11-28 16:01

I have a web app that is written using ASP.NET MVC framework. In my Homecontroller I have an action called Index which responds to a Get

3条回答
  •  一整个雨季
    2020-11-28 16:41

    Your form needs to post back to the GET method, and that method needs to include parameters for your filter properties. Your PagedListPager code in the view also need to include those filter properties so they are retained when you navigate to the next/previous page. Note that the Index() POST method is not used and can be deleted.

    Having your model contain a complex object to the filter properties and extra complexity when binding, so start by changing your model to

    public class Presenter
    {
        public IPagedList Tasks { get; set; }
        public int? Status { get; set; } // note nullable
        ... // add any other properties of TasksFiltersViewModel 
        public int PageNumber { get; set; }
        public IEnumerable Statuses { get; set; }
    }
    

    Then change the Index() method to

    public ActionResult Index(int? id, int? status) // add any other parameters your filtering on
    {
        int pageNumber = (id ?? 1);
        var tasks = db.Tasks; // IQueryable
        if (status.HasValue)
        {
            tasks = tasks.Where(x => x.Status == status.Value)
        }
        if (otherParametersHaveValue)
        {
            tasks = tasks.Where(....);
        }
        Presenter model = new Presenter()
        {
            PageNumber = id ?? 1,
            Status = status,
            .... // set any other filter properties from the parameters
            Statuses = new SelectList(...),
            Tasks = tasks.ToPagedList(pageNumber, 30)
        };
        return View(model );
    }
    

    and change the view to

    // Make the form a GET
    @using (Html.BeginForm("Index", "Tasks", FormMethod.Get, new { @class = "form-horizontal" }))
    {
        ....
        // Modify expression based on revised model properties
        @Html.DropDownListFor(m => m.Status, Model.Statuses, ...)
    }
    ....
    // Add filter parameters to url so they are retained
    @Html.PagedListPager(Model.Tasks, id => Url.Action("Index", new { id, status = Model.Status })) // add other filter properties as required
    

提交回复
热议问题