Проблема в том, что при переключении номеров страниц текст поиска, переданный действию Index, теряет исходные параметры фильтрации .
You don't need to add the currentfilter parameter to pass it.
В процессе @Html.PagedListPager
вам просто нужно reassign the searchtext
к действию Index, как показано ниже: (поскольку вы не предоставляете содержимое подробной модели, я протестировал ее с мои собственные данные)
public class DefaultController : Controller
{
private readonly MyDbContext _context;
public DefaultController(MyDbContext context)
{
_context = context;
}
public IActionResult Index(string searchText, int? page)
{
int selectedPage = page ?? 1;
// int bypassCount = (selectedPage - 1) * 4;
var pro = _context.Resent.ToList();
if (!string.IsNullOrEmpty(searchText))
{
pro = pro.Where(x => x.resent == searchText).ToList();
page = 1;
}
ViewBag.CurrentFilter = searchText;
ViewBag.products = pro.ToPagedList(selectedPage, 4);
return View();
}
}
Вид:
<form asp-action="Index" method="get">
<select class="custom-select col-md-4" asp-for="searchText">
<option value="">All</option>
<option value="AA">AA</option>
<option value="AE">AE</option>
<option value="AR">AR</option>
</select>
<div class="col-md-8">
<button class="btn btn-primary" type="submit">Search</button>
</div>
</form>
<table class="table-bordered">
<tr>
<th>Id</th>
<th>Resent</th>
<th>Resent Date</th>
<th>Created Date</th>
</tr>
@foreach (var item in ViewBag.products)
{
<tr>
<td>@item.id</td>
<td>@item.resent</td>
<td>@item.resentDate</td>
<td>@item.createDate</td>
</tr>
}
</table>
@Html.PagedListPager((IPagedList)ViewBag.products,
page => Url.Action("Index", "Default", new { page = page , searchText = ViewBag.CurrentFilter}))
Вот результат:
