Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Posted
Comments
Bohdan Stupak 23-May-17 3:36am    
you actually base your filter upon many parameters which you don't provide in @Html.PagedListPager. Is it how it supposed to be?
ammu11 23-May-17 3:45am    
Actually I don't know what to give in @Html.PagedListPager but when giving parameter and without such long parameter the filtering is not working properly
What may be the problem ??
Bohdan Stupak 23-May-17 3:49am    
I mean that you provide into public async Task<actionresult> Index much more parameters then into @Html.PagedListPager. As far as I understand the list of parameters should be the same as soon as page => Url.Action would be get request to your server which would be handled in public async Task<actionresult> Index method

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900