获取请求 .NET MVC Core 中具有多个条件的问题分页

Problem paging with multiple criteria in get request .NET MVC Core

我正在使用 Net Core MVC 结构开发 Web 应用程序。我有一个包含许多搜索条件的页面,我想通过 URL 发送参数并获取请求。我在我的视图中使用了很多 select 组件,它可以是 select 多个值。我在请求中通过 URL 发送一个或多个值。在我看来有一个分页系统,在使用过滤器值发出 get 请求后,未检索或将值设置为 select 组件。我也在使用 Select2 组件。简而言之,分页会在视图控制器之间检索或发送值时产生问题。

[HttpGet]  
    public async Task<IActionResult> TumApartmanlar(long il,long[] ilceler, int? pageNumber)
    {
        ViewData["ilceler"] = ilceler;
        ViewData["il"] = il;
        //Then set to filters

        var sonuc = _apartmanService.ListByFilters(filters, loadImages: true);

        int pageSize = 10;
        TumApartmanlarViewModel tumApartmanlar = new TumApartmanlarViewModel();
        tumApartmanlar.ApartmanListe = PaginatedList<ApartmanListDto>.Create(sonuc, pageNumber ?? 1, pageSize),
       ..... another code part and paging


        return View(tumApartmanlar);
    }

查看部分:

 <form asp-controller="EndUser" asp-action="TumApartmanlar" method="get">
                <div class="row mt-2">
                    <div class="col-12">
                        <label>İl</label>
                        <select  class="form-control" id="selectIlSec"  asp-for="@ViewData["il"]" name="il"
                                asp-items="@(new SelectList(Model.Iller,"Id","Ad"))">
                            <option></option>
                        </select>
                    </div>

                </div>
                <div class="row mt-2">
                    <div class="col-12">
                        <label>İlçe</label>
                        <select multiple="multiple" class="form-control" disabled="@(Model.ApartmanFilters.IlId>0?false:"disabled")" id="selectIlceSec" 
                                 name="ilceler"  asp-for="@ViewData["ilceler"]" asp-items="@(new MultiSelectList(Model.Ilceler,"Id","Ad"))">
                            <option></option>
                        </select>
                    </div>
                 /*Another components*/
                </div>
               <button type="submit" style="width:100%;" class="btnAra btn btn-md p-2 mt-3 mb-3 btn-danger"> Ara </button>
                

</form>
    @{
        var prevDisabled = !Model.ApartmanListe.HasPreviousPage ? "disabled" : "";
        var nextDisabled = !Model.ApartmanListe.HasNextPage ? "disabled" : "";
    }

   

    <a asp-action="TumApartmanlar"
       asp-route-pageNumber="@(Model.ApartmanListe.PageIndex - 1)"                      
       asp-route-il="@Model.ApartmanFilters.IlId"
       asp-route-ilceler="@(ViewData["ilceler"].ToString())"
       class="btn btn-default @prevDisabled">
        Previous
    </a>


    <a asp-action="TumApartmanlar"           
       asp-route-pageNumber="@(Model.ApartmanListe.PageIndex + 1)"
       asp-route-il="@Model.ApartmanFilters.IlId"
       asp-route-ilceler="@(ViewData["ilceler"].ToString())"
       class="btn btn-default @nextDisabled">
        Next
    </a>

脚本部分

$("#selectIlSec").select2({
    width: 'resolve',
    placeholder: "İl Seçiniz",
    allowClear: true
});

$("#selectIlceSec").select2({
    width: 'resolve',
    placeholder: "İlçe Seçiniz",
    allowClear: true
});

如何实现点击分页按钮时发送多个selected值?我尝试 asp-for-ilceler 但不发送值。此外,当单击按钮获取请求时,URL 看起来像 ..../EndUser/TumApartmanlar?il=1&ilceler=1&ilceler=2... 分页无法像单独的 ilceler 那样绑定。我在等你的帮助,谢谢。

更新

我的过滤器class

 public class ApartmanListFilter
 {
    [FromQuery(Name = "il")]
    public long IlId { get; set; }    

    [FromQuery(Name = "ilceler")]
    public List<long> IlceIds { get; set; }
 }

如果我像上面那样使用,我的控制器和视图会像下面这样改变,但我仍然有问题。

 [HttpGet]
    public IActionResult Evler([FromQuery]ApartmanListFilter filters)
    {
        var sonuc = _apartmanService.ListByFilters(filters, loadImages: true);
       //Another codes
        return View(sonuc);
    }

<div class="col-12">
    <label>İl</label>
      <select  class="form-control" id="selectIlSec"  asp-for="@Model.ApartmanFilters.IlId" name="il"
    asp-items="@(new SelectList(Model.Iller,"Id","Ad"))">
       <option></option>
        </select>
 </div>

解决方案 1

可以用javasript跳转,下面是例子

Previous为例:

<a class="sort"  href="javascript:void(0)">
Previous
</a>

@section scripts{
    <script>
        var c=@Html.Raw(Json.Serialize((long[])ViewData["ilceler"]));
        $(".sort").click(function () {
            var link = "/EndUser/TumApartmanlar?";
            link += "il=" + 1 +"&";
            c.forEach(function (value) {
                link += "ilceler=" + value + "&";
            });
            link += "pageNumber=" + 1;
            window.location.href = link;
        })
    </script>
}

请注意ilpageNumber是我模拟的数据,需要你自己在javascript中获取。

测试结果:

解决方案 2

您可以将您的类型 long[] 更改为 string[],然后在您的 TumApartmanlar:

 [HttpGet]
    public async Task<IActionResult> TumApartmanlar(long il, string[] ilceler, int? pageNumber)
    {
        string ids = String.Join(",", ilceler.Select(p => p.ToString()).ToArray());
        ViewData["ilceler"] = ids;
        ViewData["il"] = il;

然后当你点击a标签时,你的ilceler会变成string[0]="1,3", 然后您可以将数据处理成您想要的格式。

测试结果: