I have written the following Code to implement sorting. I want to maintain the sortingData(OrderDirection,SortField) between two requests for Sorting. Somehow I'm not able to achieve that.
//In .cshtml Page
@{
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>
-----In cshtml.cs Page
The get Request is as below:
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();
}
I'm trying to pass the object and maintain it in ViewData so that It could be accessed. But everytime, only null value is returned. Is there any better way for implementing Sorting with Razor Pages or If this could be made to work?
<th>tags look like? - bommelding