我有一个
Index
Razor 页面,根据教程这里实现了分页。我还实现了用户可选择的页面大小调整(为简洁起见,此处未显示详细信息,因为它不适用于此问题)以及我都在工作的内容。我正在尝试从同一页面here添加搜索功能。我也有搜索功能。
我正在努力工作的是,当单击搜索按钮时,将当前页面大小路由到 Get 方法。
这是我在索引页上的搜索表单:
<form asp-page="./Index" asp-route-pageSize="@Model.Logs.PageSize" method="get">
<div class="form-actions no-color">
<p>
Find by @nameof(Data.Audit.Log.Level):
<select asp-for="@Model.LevelFilter" asp-items="Html.GetEnumSelectList<Microsoft.Extensions.Logging.LogLevel>()">
<option selected="selected" value="@Model.LevelFilter">Please select</option>
</select>
<input type="submit" value="Search" class="btn btn-primary" asp-route-pageSize="@Model.Logs.PageSize" /> |
<a asp-page="./Index" asp-route-pageSize="@Model.Logs.PageSize">Back to full List</a>
</p>
</div>
</form>
这是
Get
页面上的Index
方法:
public PaginatedList<Data.Audit.Log> Logs { get; set; }
public LogLevel? LevelFilter { get; set; }
public async Task OnGetAsync(int? pageIndex, int? pageSize, int? prevPageSize, LogLevel? levelFilter)
{
LevelFilter = levelFilter;
if (prevPageSize != null && pageSize != null && pageIndex != null && pageIndex > 1)
{
pageIndex = PaginatedList<Data.Audit.Log>.CalculateNewPageIndex(pageIndex.Value, prevPageSize.Value, pageSize.Value);
}
var query = DbContext.Logs.AsQueryable();
if(levelFilter != null)
{
query = query.Where(x => x.Level == levelFilter.ToString());
}
Logs = await PaginatedList<Data.Audit.Log>.CreateAsync(query.OrderByDescending(x => x.Logged).AsNoTracking(), pageIndex ?? 1, pageSize ?? _defaultPageSize);
}
正如您在
.cshtml
标记中看到的,我尝试将 asp-route-pageSize
放在表单、输入,甚至表单中的“a”标签上。 html 似乎已正确生成:
“返回完整列表”行为正确并保持页面大小,但“搜索”按钮会丢失它。此场景中添加的唯一查询参数是 levelFilter。有什么想法我错过/做错了吗?
更新:这是整个
.cshtml
供参考:
<h1>@ViewData["Title"]</h1>
<form asp-page="./Index" asp-route-pageSize="@Model.Logs.PageSize" method="get">
<div class="form-actions no-color">
<p>
Find by @nameof(Data.Audit.Log.Level):
<select asp-for="@Model.LevelFilter" asp-items="Html.GetEnumSelectList<Microsoft.Extensions.Logging.LogLevel>()">
<option selected="selected" value="@Model.LevelFilter">Please select</option>
</select>
<input type="submit" value="Search" class="btn btn-primary" asp-route-pageSize="@Model.Logs.PageSize" /> |
<a asp-page="./Index" asp-route-pageSize="@Model.Logs.PageSize">Back to full List</a>
</p>
</div>
</form>
<table class="usa-table usa-table--striped usa-table--borderless">
<thead>
<tr>
<th scope="col">
@Html.DisplayNameFor(model => model.Logs[0].Logged)
</th>
<th scope="col">
@Html.DisplayNameFor(model => model.Logs[0].Level)
</th>
<th scope="col">
@Html.DisplayNameFor(model => model.Logs[0].Message)
</th>
<th scope="col">
@Html.DisplayNameFor(model => model.Logs[0].UserName)
</th>
<th scope="col">
@Html.DisplayNameFor(model => model.Logs[0].Category)
</th>
<th>
Action
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.Logs)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Logged)
</td>
<td>
@Html.DisplayFor(modelItem => item.Level)
</td>
<td style="white-space:normal">
@Html.DisplayFor(modelItem => item.Message)
</td>
<td>
@Html.DisplayFor(modelItem => item.UserName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Category)
</td>
<td>
<a asp-page="./Details" asp-route-id="@item.Id" class="usa-button usa-button--outline">Details</a>
</td>
</tr>
}
</tbody>
</table>
<div id="pagingDiv" class="pages-div">
@if (Model.Logs.HasPrevPage)
{
<a asp-page="./Index" asp-route-pageIndex="@(Model.Logs.PageIndex - 1)" asp-route-pageSize="@Model.Logs.PageSize" asp-route-levelFilter="@Model.LevelFilter" class="usa-button usa-button--outline">Prev</a>
}
@if (Model.Logs.ShowFirstPage)
{
<a asp-page="./Index" asp-route-pageIndex="@(1)" asp-route-pageSize="@Model.Logs.PageSize" asp-route-levelFilter="@Model.LevelFilter" class="usa-button usa-button--outline">1</a>
<div class="inline-div">...</div>
}
@for (int i = Model.Logs.MinPage; i <= Model.Logs.MaxPage; i++)
{
if (Model.Logs.PageIndex == i)
{
<div class="non-button usa-button usa-button--active">@i.ToString()</div>
}
else
{
<a asp-page="./Index" asp-route-pageIndex="@i" asp-route-pageSize="@Model.Logs.PageSize" asp-route-levelFilter="@Model.LevelFilter" class="usa-button usa-button--outline">@i.ToString()</a>
}
}
@if (Model.Logs.ShowLastPage)
{
<div class="inline-div">...</div>
<a asp-page="./Index" asp-route-pageIndex="@Model.Logs.TotalPages" asp-route-pageSize="@Model.Logs.PageSize" asp-route-levelFilter="@Model.LevelFilter" class="usa-button usa-button--outline">@Model.Logs.TotalPages</a>
}
@if (Model.Logs.HasNextPage)
{
<a asp-page="./Index" asp-route-pageIndex="@(Model.Logs.PageIndex + 1)" asp-route-pageSize="@Model.Logs.PageSize" asp-route-levelFilter="@Model.LevelFilter" class="usa-button usa-button--outline">Next</a>
}
</div>
<div id="pagingDiv" class="records-per-page-div">
@foreach(var i in Model.Logs.PageSizes)
{
if (Model.Logs.PageSize == i)
{
<div class="non-button usa-button usa-button--active">@i.ToString()</div>
}
else
{
<a asp-page="./Index" asp-route-pageIndex="@Model.Logs.PageIndex" asp-route-pageSize="@i" asp-route-prevPageSize="@Model.Logs.PageSize"
asp-route-levelFilter="@Model.LevelFilter" class="usa-button usa-button--outline">@i.ToString()</a>
}
}
</div>
在@pcalkins的大力帮助下,这是正确的答案。我必须添加映射到 Model.Logs.PagSize 的隐藏输入标记,并为其指定查询字符串中所需的参数名称:
<input asp-for="@Model.Logs.PageSize" name="pageSize" type="hidden" />
完整表格如下:
<form asp-page="./Index" method="get">
<div class="form-actions no-color">
<input asp-for="@Model.Logs.PageSize" name="pageSize" type="hidden" />
<p>
Find by @nameof(Data.Audit.Log.Level):
<select asp-for="@Model.LevelFilter" asp-items="Html.GetEnumSelectList<Microsoft.Extensions.Logging.LogLevel>()">
<option selected="selected" value="@Model.LevelFilter">Please select</option>
</select>
<input type="submit" value="Search" class="btn btn-primary" /> |
<a asp-page="./Index" asp-route-pageSize="@Model.Logs.PageSize">Back to full List</a>
</p>
</div>
</form>