I am doing a project and when searching based on from and to searching, it filters but on selecting the second page after giving paging the filter that given losses and shows all the items from db.
controller
public async Task<ActionResult> Index(string sortOrder, string CurrentSort, string currentFilter, string searchString, int? page, string FromDateString, string ToDateString)
{
int pageSize = 10;
int pageIndex = 1;
pageIndex = page.HasValue ? Convert.ToInt32(page) : 1;
ViewBag.CurrentSort = sortOrder;
sortOrder = String.IsNullOrEmpty(sortOrder) ? "DateTime" : sortOrder;
IPagedList<PhoneNo> Phone= null;
var List = db.PhoneNos.ToList();
if (!String.IsNullOrEmpty(searchString))
{
List = List .Where(s => s.PhoneNum.ToString().Contains(searchString)).ToList();
}
if (!String.IsNullOrEmpty(FromDateString) && String.IsNullOrEmpty(ToDateString))
{
List = List .Where(s => s.DateTime >= FromDateString.ToDateFromString()).ToList();
}
if (!String.IsNullOrEmpty(ToDateString) && String.IsNullOrEmpty(FromDateString))
{
List = List .Where(s => s.DateTime <= ToDateString.ToDateFromString()).ToList();
}
if (!String.IsNullOrEmpty(ToDateString) && !String.IsNullOrEmpty(FromDateString))
{
List = List .Where(s => s.DateTime >= FromDateString.ToDateFromString() && s.DateTime <= ToDateString.ToDateFromString()).ToList();
}
switch (sortOrder)
{
case "DateTime":
if (sortOrder.Equals(CurrentSort))
Phone= List .OrderByDescending(m => m.DateTime).ToPagedList(pageIndex, pageSize);
else
Phone= List .OrderBy(m => m.DateTime).ToPagedList(pageIndex, pageSize);
break;
case "Default":
Phone= List .OrderBy(m => m.DateTime).ToPagedList(pageIndex, pageSize);
break;
}
return View(Phone);
}
this is my cntroller and searching based on fromdatestring and todatestring
in view page the paging session is
<div style="text-align:left;">
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber)
of @Model.PageCount
@Html.PagedListPager(Model, page => Url.Action("Index", new { page, currentFilter = ViewBag.CurrentFilter, sortOrder = ViewBag.sortOrder }))
</div>
this is my view page and my problem is the filtering losses when selecting the second page in paging . how can I correct my issue ?? can anyone please help me to find the solution??
What I have tried:
I have searched and only find for a single search but didn't get the search for two differnt string