Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Does not hold selected value AA while using pagination. When moving to another page, dropdownlist moves to state All.

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 enlighten me the way to do this.

What I have tried:

Public async Task<IActionResult> Index(string searchText, string currentFilter, int? page)
    {

            int selectedPage = page ?? 1;
            int bypassCount = (selectedPage - 1) * _pagingOptions.PageSize;

            if (searchText != null)
            {
                page = 1;
            }
            else
            {
                searchText = currentFilter;

            }

            ViewBag.CurrentFilter = searchText;

    }


Index

<form asp-action="Index" method="get">

            <select class="custom-select" asp-for="searchText" value="@(ViewBag.CurrentFilter)">
                <option value="">All</option>
                <option value="AA">AA</option>
                <option value="AE">AE</option>
                <option value="AR">AR</option>
            </select>

         <div class="col-md-12">
            <button class="btn btn-primary" type="submit">Search</button>
         </div>                
        </form>

    <table class="table">
        <thead>
            <tr >
                <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 => 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})
            }
Posted
Updated 2-Apr-20 10:31am

1 solution

Two problems:

1) The <select> tag does not contain a value attribute. Instead, you have to set the selected attribute on the selected <option> element.
Razor
<option value="AA" @(ViewBag.CurrentFilter == "AA" ? "selected" : "")>AA</option>
Annoyingly, if the option is considered selected if the attribute is present, regardless of the value, so you need to omit the attribute entirely on options which should not be selected.


2) The pager links are generating links, rather than submitting the form. If you change the selected value in the list, it will not be sent to the server.

You will need to use Javascript to either update the page links when the selected item changes; submit the form when the selected item changes; or submit the form when a page link is clicked.

Either that, or find a way to make PagedListPager render submit buttons instead of links.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900