I have only found tutorials of paging + searching which include some libraries. I want to make a custom pageable and searchable view for a basic model.

Model:

public class Person()
{
   public string Name {get;set;}

   public int Age {get;set;}
}

Action

public ActionResult Filter(int page = 1, string search, int age)
{
  int ItemsPerPage = 5; 

  var peopleCount = peopleServices.All().Count();

  int totalPages = (int)Math.Ceiling(peopleCount/ (decimal)ItemsPerPage);
  int itemsToSkip = (page - 1) * ItemsPerPage;

  var people = this.peopleServices.All();

   if (!string.IsNullOrEmpty(search))
   {
       people = people.Where(x => x.Name == search).ToList();
   }

   if (age != null)
   {
       people = people.Where(x => x.Age > age).ToList();
   }

   return this.View(people);
}

View:

@using (Html.BeginForm("Filter", "Home", FormMethod.Get))
{
  @Html.TextBox("search")
  @Html.TextBox("age")
  <input type="submit" value="search" />

  // Listing the returned people here

  @for (int i = 1; i <= totalPages; i++)
  {
     <li>
       <a href="/Home/Filter/@i">@i</a>
     </li>
  }

}

If I click search it will get me the people with the requested username and age. Problem is that if I click on some of the anchor tags it would only redirect me to the view with page info without search and age, because I don't specify them in the href. So how can I make it work together ? And if it's possible to also choose to order them by age(both descending and ascending options) ?

Related posts

Recent Viewed