How to I Apply Filter While Paginating in ASP.NET MVC and Entity Framework

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

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<Task> Tasks { get; set; }
public int? Status { get; set; } // note nullable
... // add any other properties of TasksFiltersViewModel
public int PageNumber { get; set; }
public IEnumerable<SelectListItem> 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<Task>
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

Pagination using Entity Framework through Web API

NotMapped is what I was looking for. I hope this will surely help somebody.

public class Paging
{
[NotMapped]
public Int16 StartPage { get; set; }

[NotMapped]
public Int16 PageSize { get; set; }
}

public class BE_CategoryBase : Paging
{
public Int32 CategoryID { get; set; }
public String CategoryName { get; set; }
public String CategorySanitized { get; set; }
public Boolean IsActive { get; set; }
public DateTime? ModificationDate { get; protected set; }
public Int64? ModifiedBy { get; set; }
}

ASP.NET MVC. PagedList filtering by multiple params

You need to use ajax call instead of helper. Write ajax call and pass parameters as post method.

paging in entity framework using asp.net mvc application (using aspx)

You need to use ASP.NET Server tags <%:-- %>

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"    Inherits="System.Web.Mvc.ViewPage<PagedList.IPagedList<ContosoUniversity.Models.Student>>" %>
<%@ Import Namespace="PagedList.Mvc" %>

<%:(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 }))%>

EDIT :

Details on using inline server tags :
http://support.microsoft.com/kb/976112

How to query with entity and fitlering and pagedlist with one milion rows

You should read difference between IEnumerable and IQueryable

In your case you need to first make the filter query using IQueryable

IQueryable<works> worksDetails = db.works.Where(x => x.Status == 100)
.OrderByDescending(x => x.ID_Work); // just creates the sql query to filter records

Finally hit the database to get records using PagedList

var pagedrows = worksDetails.ToPagedList(pageNumber, pageSize); 
// hits the database and executes the sql query to filter records

Hope this helps.



Related Topics



Leave a reply



Submit