在ASP.NET核心MVC中,在分页列表的下一页保留HttpPost中的Filter



我正在创建一个程序,其中有一个包含要筛选的记录的数据库。滤波器具有4个输入类型="0";文本";并根据我键入的筛选器返回正确的记录。问题是,当我点击(PaginatedList的(下一页链接时,第二页没有调整过滤器。它失去了我提供的过滤器。我知道我必须在"下一页"链接上给asp路由一些变量,其中包含像Microsoft Tutorial给currentFilter这样的过滤器中给出的数据,但我有4个这样的过滤器:名称=x&电话=是(amp;…(;。。。

这是我的控制器代码简化:

public  async Task<IActionResult> Index( some variables ){ 
var appusers = from a in _context.AppUsers
select a;
return(return View(await app.PaginatedList<AppUser>.CreateAsync(appusers.AsNoTracking(), pageNumber ?? 1, pageSize));)
}

AND
[HttpPost]
public async Task<IActionResult> Index(string Name, string Phone, string Calls, string Date){

var ap = from c in _context.AppUsers
select c;
Filter The database based on variable values Name,Phone,Calls,Date
return View(await PushNotificationApp.PaginatedList<AppUser>.CreateAsync(ap.AsNoTracking(), 
pageNumber?? 1, pageSize));}

这是索引的一部分:

@using (Html.BeginForm("Index", "AppUsers", new { id = "filterForm" }))
{
name="SearchTrips" value="@ViewData["CurrentTrips"]" />
<input id="NameFilterField" type="text" name="Name" />
<input id="PhoneFilterField" type="text" name="Phone" />
<input id="CallsFilterField" type="text" name="Calls" />
<input id="DateFilterField" type="text" pattern="d{1,2}/d{1,2}/d{4}" name="Date" />

<button class="m-3" type="submit" value="submit">Search</button>
}
AND PAGELIST LINK PART :
<div class="p-2 bd-highlight">
<nav aria-label="Page navigation example">
<ul class="pagination">
<li class="page-item">
<a class="page-link MainButton" asp-action="Index"
asp-route-pageNumber="@(Model.PageIndex - 1)"
asp-route-currentFilter="@ViewData["CurrentFilter"]"       
class="page-link @prevDisabled">
Previous
</a>
</li>
<li class="page-item">
<a class="page-link MainButton" asp-action="Index"
asp-route-pageNumber="@(Model.PageIndex + 1)"
asp-route-currentFilter="@ViewData["CurrentFilter"]"                         
Next
</a>
</li>
</ul>
</nav>
</div>

问题是,当我单击(PaginatedList的(下一页链接时,第二页没有调整过滤器。它失去了我提供的过滤器。

您正在Post操作中进行筛选,但当您单击"下一步"链接时,它将转到不进行筛选的Get操作。所以,我认为你可以让表单做一个get请求,只需要在get方法中做过滤器。

我知道我必须在"下一页"链接上给asp路由一些变量,其中包含过滤器中给定的数据,就像微软教程给currentFilter一样,但我有4个过滤器,如下所示:Name=x&电话=是(amp;…(;。。。

4个滤波器是相同的,只是将其他滤波器参数提供给Next链路

<a class="page-link MainButton" asp-action="Index"
asp-route-pageNumber="@(Model.PageIndex + 1)"
asp-route-Name="@ViewData["Name"]"
asp-route-Phone="@ViewData["Phone"]"
asp-route-Calls="@ViewData["Calls"]"
asp-route-Date="@ViewData["Date"]"
Next
</a>

为什么不使用一个框来搜索所有内容,或者使用教程中下面的方法使用四个搜索框?

if (!String.IsNullOrEmpty(searchString))
{
students = students.Where(s => s.LastName.Contains(searchString)
|| s.FirstMidName.Contains(searchString));
}

最新更新