Reputation: 679
Checkbox does not retains checked status while using pagination. When moving to another page, why checkbox unchecks.
My pagination is working fine. The only issue is to maintain checkbox checked status.
Used ViewBag to store the current filter state but no luck.
No idea where to go and change the code to make it work. Can anyone put some lights on it.
Controller
Public async Task<IActionResult> Index(bool searchText, bool currentFilter, int? page)
{
int selectedPage = page ?? 1;
int bypassCount = (selectedPage - 1) * _pagingOptions.PageSize;
if (searchText != false)
{
page = 1;
}
else
{
searchText = currentFilter;
}
ViewBag.CurrentFilter = searchText;
}
index
<form asp-action="Index" method="get">
<input type="checkbox" asp-for="searchText" class="form-control" />
<div class="col-md-12">
<button class="btn btn-primary" type="submit">Search</button>
</div>
</form>
<table class="table">
<thead>
<tr >
<th>Message Id</th>
<th>Status</th>
<th>Resent</th>
<th>Resent Date</th>
<th>Created Date</th>
</tr>
</thead>
<tbody>
@if (Model.Items.TotalItemCount > 0)
{
@foreach (var item in Model.Items.ToList())
{
<td>@Html.DisplayFor(modelItem => item.MessageId)</td>
<td>@Html.DisplayFor(modelItem => item.Status)</td>
<td>@Html.DisplayFor(modelItem => resentString)</td>
<td>@Html.DisplayFor(modelItem => resentDateString)</td>
<td>@Html.DisplayFor(modelItem => createdDateString)</td>
</tr>
}
}
</tbody>
</table>
</div>
@if (Model.Items.PageCount > 1)
{
@Html.PagedListPager(Model.Items, page => Url.Action("Index", new { page = page, currentFilter = ViewBag.CurrentFilter}),
new PagedListRenderOptions
{
UlElementClasses = new string[] { "pagination", "justify-content-left" },
LiElementClasses = new string[] { "page-item" },
PageClasses = new string[] { "page-link" },
LinkToPreviousPageFormat = "Previous",
LinkToNextPageFormat = "Next",
DisplayEllipsesWhenNotShowingAllPageNumbers = true,
DisplayLinkToPreviousPage = PagedListDisplayMode.Always,
DisplayLinkToNextPage = PagedListDisplayMode.Always
})
}
Upvotes: 0
Views: 659
Reputation: 27538
To check the checkbox based on specific value , for example , based on CurrentFilter
, you can try with below codes :
@{
int status = ViewBag.CurrentFilter ? 1 : 0;
}
<input type="checkbox" checked="@(1 == status)" class="form-control" />
Reference : https://www.learnrazorpages.com/razor-pages/forms/checkboxes
Upvotes: 1