微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

当我单击第2页或无法应用第2页的过滤器时,过滤器将重置

如何解决当我单击第2页或无法应用第2页的过滤器时,过滤器将重置

我创建了一个使用分页页面。问题是当我应用过滤器时。它仅适用于第一页;当我单击第二页时,过滤器将重置。如果我在第二页上应用过滤器,则会带我回到第一页。

以下代码适用于我将在其中应用过滤器的索引页。

<nav aria-label="DemandPaging">
    @await this.Component.InvokeAsync("Pager",new { pagingList = this.Model.Esas })
</nav>

<nav aria-label="Paging">
    <vc:pager paging-list="@Model" />
</nav>
<form asp-controller="Esa" asp-action="Index" method="post">
    <div>
        @Html.Label("Filter By:")
        @Html.DropDownListFor(model => model.SelectedStatus,new SelectList(Enum.GetValues(typeof(Status))),"")
        <input type="submit" value="Enter" />
    </div>
    <div class="table-responsive">
        <table class="table">
            <thead>
                <tr>
                    <th>
                        @Html.displayNameFor(model => model.EsaProjectId)
                    </th>
                    <th>
                        @Html.displayNameFor(model => model.EsaProjectName)
                    </th>
                    <th>
                        @Html.displayNameFor(model => model.EmpName)
                    </th>
                    <th>
                        @Html.displayNameFor(model => model.Designation)
                    </th>
                    <th>
                        @Html.displayNameFor(model => model.StartDate)
                    </th>
                    <th>
                        @Html.displayNameFor(model => model.EndDate)
                    </th>
                    <th>
                        @Html.displayNameFor(model => model.Billability)
                    </th>
                    <th>
                        @Html.displayNameFor(model => model.Location)
                    </th>
                    <th>
                        @Html.displayNameFor(model => model.Allocation)
                    </th>
                    <th>
                        @Html.displayNameFor(model => model.NblReason)
                    </th>
                    <th>
                        @Html.displayNameFor(model => model.Comments)
                    </th>
                    <th>  @Html.displayNameFor(model => model.Status)</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                @foreach (var item in Model.Esas)
                {
                    <tr>
                        <td>
                            @Html.displayFor(modelItem => item.EsaProjectId)
                        </td>
                        <td>
                            @Html.displayFor(modelItem => item.EsaProjectName)
                        </td>
                        <td>
                            @Html.displayFor(modelItem => item.EmpName)
                        </td>
                        <td>
                            @Html.displayFor(modelItem => item.Designation)
                        </td>
                        <td>
                            @Html.displayFor(modelItem => item.StartDate)
                        </td>
                        <td>
                            @Html.displayFor(modelItem => item.EndDate)
                        </td>
                        <td>
                            @Html.displayFor(modelItem => item.Billability)
                        </td>
                        <td>
                            @Html.displayFor(modelItem => item.Location)
                        </td>
                        <td>
                            @Html.displayFor(modelItem => item.Allocation)
                        </td>
                        <td>
                            @Html.displayFor(modelItem => item.NblReason)
                        </td>
                        <td>
                            @Html.displayFor(modelItem => item.Comments)
                        </td>
                        <td>
                            @Html.displayFor(modelItem => item.Status)
                        </td>
                        <td>
                            @*<a asp-action="Edit" asp-route-id="@item.CtsEmpId">Edit</a> |
                                <a asp-action="Details" asp-route-id="@item.CtsEmpId">Details</a> |
                                <a asp-action="Delete" asp-route-id="@item.CtsEmpId">Delete</a>*@
                        </td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
</form>

控制器如下:

  public async Task<IActionResult> Index(Esaviewmodel viewmodel,int page = 1)
    {
        int pageSize = 8;
        var query = _context.Esas.AsNoTracking()
                 .OrderBy(s => s.EsaProjectId);
        Status result = viewmodel.SelectedStatus;
        if (User.IsInRole("Admin") || User.IsInRole("PMOUser"))
        {
            if (viewmodel.SelectedStatus.ToString() == "")
            {
                query = from x in _context.Esas
                        orderby x.Status descending
                        select x;
            }
            else
            {
                query = _context.Esas.AsNoTracking()
                         .Where(s => s.Status == viewmodel.SelectedStatus.ToString())
                         .OrderBy(s => s.Status);
            }

        }

        viewmodel.Esas = await PagingList.CreateAsync(query,pageSize,page);
        return View(viewmodel);

可以请任何人帮助我吗?我该如何保留过滤器?

解决方法

在您的情况下,请在第二页上应用过滤器,这样会将您带回到第一页。

我有一种直觉,当您提交过滤器时,page参数为null-> page为1

因此,我想您在表单中放置了一个隐藏字段,该位置存储了当前页面

(ViewContext.HttpContext.Request.QueryString [“ page”]:获取参数 名称是您网址中的页面。例如:http:// localhost /?page = 2)

->它将发送到控制器并使page参数不为空。

<form asp-controller="Esa" asp-action="Index" method="post">
    <div>
        //Get value page in your url
        @Html.Hidden("page",ViewContext.HttpContext.Request.QueryString["page"])
        @Html.Label("Filter By:")
        @Html.DropDownListFor(model => model.SelectedStatus,new SelectList(Enum.GetValues(typeof(Status))),"")
        <input type="submit" value="Enter" />
    </div>
    <div class="table-responsive">
        <table class="table">
            <thead>
                <tr>
                    <th>
                        @Html.DisplayNameFor(model => model.EsaProjectId)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.EsaProjectName)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.EmpName)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.Designation)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.StartDate)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.EndDate)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.Billability)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.Location)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.Allocation)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.NblReason)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.Comments)
                    </th>
                    <th>  @Html.DisplayNameFor(model => model.Status)</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                @foreach (var item in Model.Esas)
                {
                    <tr>
                        <td>
                            @Html.DisplayFor(modelItem => item.EsaProjectId)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.EsaProjectName)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.EmpName)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.Designation)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.StartDate)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.EndDate)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.Billability)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.Location)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.Allocation)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.NblReason)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.Comments)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.Status)
                        </td>
                        <td>
                            @*<a asp-action="Edit" asp-route-id="@item.CtsEmpId">Edit</a> |
                                <a asp-action="Details" asp-route-id="@item.CtsEmpId">Details</a> |
                                <a asp-action="Delete" asp-route-id="@item.CtsEmpId">Delete</a>*@
                        </td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
</form>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。