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

如何在ASP.NET Core MVC中使用下拉列表更新每页显示的元素数?

如何解决如何在ASP.NET Core MVC中使用下拉列表更新每页显示的元素数?

这是我的控制器动作的代码。当前,我正在硬编码要显示的项目数2。我想从下拉列表中选择项目数,以便当用户选择一个数字(例如10)时视图将每页显示十个项目。因为我是新手,所以请有人帮助我,并且没有任何与此相关的解决方案,这在我的学期项目中是必需的。我将非常感谢。

 public IActionResult Index(string sortOrder,int pageNumber=1,int pageSize=2)
    {
        ViewBag.CurrentSortOrder = sortOrder;
        ViewBag.NameSortParam =String.IsNullOrEmpty(sortOrder)? "name_desc":"";
        int ExcludeRecords = (pageSize * pageNumber) - pageSize;
        var Students = from b in _context.Students.Where(x => x.IsDeleted == false).ToList()
                       select b;

        switch (sortOrder)
            {
               case "name_desc":
                 Students= Students.OrderByDescending(a => a.Name).ToList();
                   break;
               default:
                   Students = Students.OrderBy(a => a.Name).ToList();
                   break;
           }


        Students = Students.Skip(ExcludeRecords)
            .Take(pageSize);
        var result = new PagedResult<Student>
        {
            Data = Students.ToList(),TotalItems = _context.Students.Where(x => x.IsDeleted == false).Count(),PageNumber=pageNumber,PageSize = pageSize
        };
       
        return View(result);
    }

这是寻呼机代码,我正在使用cloudscribe分页

<cs-pager cs-paging-pagenumber="(int)Model.PageNumber"
      cs-paging-totalitems="(int)Model.TotalItems"
      cs-paging-pagesize="Model.PageSize"
      cs-pagenumber-param="pagenumber"
      asp-route-sortOrder="@ViewBag.CurrentSortOrder"
      asp-controller=@ViewData["Controller"]
      asp-action=@ViewData["Action"]
      cs-pager-li-current-class="page-item active"
      cs-pager-li-other-class="page-item"
      cs-pager-li-non-active-class="page-item disabled"
      cs-pager-link-current-class="page-link"
      cs-pager-link-other-class="page-link">

解决方法

您需要添加pageSize参数以传递给cs-pager中的操作,每次保存通过下拉列表选择的pageSize值,然后将其绑定回数据。

首先使用ViewBag.pageSize = pageSize;从视图中保存pageSize

然后在视图中使用此代码先获取所选的pageSize:

   @{ 
        var selectedIndex = (int)ViewBag.pageSize;
    }

最后,在cs-pager中添加asp-route-pageSize="@selectedIndex"

以下是完整的代码供您参考:

查看:

@{ 
    var selectedIndex = (int)ViewBag.pageSize; //add this line
}

@model xxx

<h1>Index</h1>

<form action="/Controller/Action" method="POST">
    <select name="pageSize" asp-for="@selectedIndex"  onchange="javascript:this.form.submit()">
        <option>please choose a number</option>
        <option value=1>1</option>
        <option value=2>2</option>
        <option value=3>3</option>
        <option value=4>4</option>
        <option value=5>5</option>
        <option value=6>6</option>
        <option value=7>7</option>
        <option value=8>8</option>
        <option value=9>9</option>
    </select> 
</form>



<table class="table table-striped table-bordered">
    <thead>
        <tr>
            <th>Studentname</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var a in Model.Students.Data)
        {
            <tr>
                <td>@a.Name</td>
            </tr>
        }
    </tbody>
</table>

<cs-pager cs-paging-pagenumber="(int)Model.PageNumber"
      cs-paging-totalitems="(int)Model.TotalItems"
      cs-paging-pagesize="Model.PageSize"
          cs-pagenumber-param="pagenumber"
          asp-route-sortOrder="@ViewBag.CurrentSortOrder"
          asp-controller="@ViewData["Controller"]"
          asp-action="ViewData["Action"]"  asp-route-pageSize="@selectedIndex"
          cs-pager-li-current-class="page-item active"
          cs-pager-li-other-class="page-item"
          cs-pager-li-non-active-class="page-item disabled"
          cs-pager-link-current-class="page-link"
          cs-pager-link-other-class="page-link"></cs-pager>  

动作:

 public IActionResult Index(string sortOrder,int pageNumber=1,int pageSize=1)
    { 
        ViewBag.pageSize = pageSize;// add this line,it is the key point
        ViewBag.CurrentSortOrder = sortOrder;
        ViewBag.NameSortParam =String.IsNullOrEmpty(sortOrder)? "name_desc":"";
        int ExcludeRecords = (pageSize * pageNumber) - pageSize;
        var Students = from b in _context.Students.Where(x => x.IsDeleted == false).ToList()
                       select b;

        switch (sortOrder)
            {
               case "name_desc":
                 Students= Students.OrderByDescending(a => a.Name).ToList();
                   break;
               default:
                   Students = Students.OrderBy(a => a.Name).ToList();
                   break;
           }


        Students = Students.Skip(ExcludeRecords)
            .Take(pageSize);
        var result = new PagedResult<Student>
        {
            Data = Students.ToList(),TotalItems = _context.Students.Where(x => x.IsDeleted == false).Count(),PageNumber=pageNumber,PageSize = pageSize
        };
       
        return View(result);
    }

这是测试结果:

enter image description here

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