【发布时间】:2019-03-09 22:11:44
【问题描述】:
我编写了以下代码来实现排序。我想在两个排序请求之间维护sortingData(OrderDirection,SortField)。不知何故,我无法做到这一点。
//在.cshtml页面中
@{
SortingPagingInfo info = (SortingPagingInfo)ViewData["SortingPagingInfo"];
}
<form method="post">
@Html.Hidden("SortField", info.SortField)
@Html.Hidden("SortDirection", info.SortDirection)
@Html.Hidden("PageCount", info.PageCount)
@Html.Hidden("PageSize", info.PageSize)
@Html.Hidden("CurrentPageIndex", info.CurrentPageIndex)
<table class="table">
<thead>
<tr>
<th>
<a asp-page="./Page" asp-route-sortData="@info">
@Html.DisplayNameFor(model => model.Tabl[0].Col1)
</a>
</th>
<th>
<a asp-page="./Page" asp-route-sortData="@info">
@Html.DisplayNameFor(model => model.Tabl[0].Col2)
</a>
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.Table)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Col1)
</td>
<td>
@Html.DisplayFor(modelItem => item.Col2)
</td>
</tr>
}
</tbody>
</table>
</form>
-----在cshtml.cs页面中
获取请求如下:
public async Task OnGetAsync(SortingPagingInfo sortData)
{
SortingPagingInfo info = new SortingPagingInfo();
if (string.IsNullOrEmpty(sortData.SortDirection))
{
info.SortField = "Col1";
info.SortDirection = "OrderBy";
info.PageSize = 10;
info.PageCount = Convert.ToInt32(Math.Ceiling((double)(_context.Tab.Count()
/ info.PageSize)));
info.CurrentPageIndex = 0;
this.sortPageData = info;
ViewData["SortingPagingInfo"] = info;
}
else
{
info = (SortingPagingInfo)ViewData["SortingPagingInfo"];
}
tab1= await _context.Tabl.OrderByDynamic(info.SortField, info.SortDirection).ToListAsync();
}
我正在尝试传递对象并将其保存在 ViewData 中,以便可以访问它。但每次都只返回空值。有没有更好的方法来实现使用 Razor 页面进行排序,或者如果这可以工作?
【问题讨论】:
-
<th>标记中的这些 URL 是什么样的?
标签: c# sorting asp.net-core razor-pages