【问题标题】:Issues while implementing Paging in ASP.net MVC在 ASP.net MVC 中实现分页时的问题
【发布时间】:2013-12-15 06:28:50
【问题描述】:

在我之前的问题中,我询问了在根据找到的记录数实现分页、搜索和更新页数时的相关问题。我是 MVC 的新手。我正在从数据库中检索记录,并根据计算的页数,这工作正常,但主要问题是当我从数据库中搜索特定记录时,页数没有更新,它们与所有记录保持相同.请帮忙解决。谢谢...

Product.cshtml

@model IEnumerable<SearchRecord.Models.tbl_product>
<html>
<head>
    <script src="@Url.Content("~/Scripts/jquery-1.5.1.js")" type="text/javascript"></script>
    <link href="@Url.Content("~/bootstrap/bootstrap.min.css")" rel="stylesheet" type="text/css" />
    <title>Index</title>
    <script type="text/javascript">
        $(document).ready(function () {
            $('#Button1').click(function () {
                $.ajax({
                    type: 'POST',
                    contentType: "application/json; charset=utf-8",
                    url: 'Home/Index',
                    data: "{'searchString':'" + document.getElementById('searchString').value + "'}",
                    async: false,
                    success: function (response) {
                        $('#showData').html(response)
                    },
                    error: function () { alert("error"); }
                });
            });
        });
    </script>
</head>
<body>
    @Html.TextBox("searchString")
    <input type="button" value="filter" id="Button1" />
    <table id="showData">
        @{Html.RenderPartial("ViewProduct");}
    </table>

</body>
</html>

HomeController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SearchRecord.Models;

namespace SearchRecord.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        AdventureWorkEntities db = new AdventureWorkEntities();
        const int pageSize = 10;

        [HttpGet]
        public ActionResult Products(int page = 1)
        {
            var products = db.tbl_product.OrderBy(p => p.ProductId).Skip((page - 1) * pageSize).Take(pageSize).ToList();
            ViewBag.CurrentPage = page;
            ViewBag.PageSize = pageSize;
            ViewBag.TotalPages = Math.Ceiling((double)db.tbl_product.Count() / pageSize);
            return View(products);
        }

  [HttpPost]
public ActionResult Index(string searchString)
{
    int page = 1;
    var query = db.tbl_product.OrderBy(p=>p.ProductName).Skip((page-1) * pageSize).Take(pageSize).ToList().Where(p => p.ProductName.Contains(searchString));
    ViewBag.TotalPages = Math.Ceiling((double)db.tbl_product.Where(c => c.ProductName.Contains(searchString)).Count() / pageSize);

    return PartialView("Viewproduct", query.ToList());
}
    }
}

ViewProduct.cshtml(部分视图)

@model IEnumerable<SearchRecord.Models.tbl_product>
@{int i = 1;}
@foreach (var p in Model)
{
    <tr class="@(i++ % 2 == 0 ? "highlighted" : "")">
        <td>@p.ProductId
        </td>
        <td>@p.ProductName
        </td>
    </tr>
}
<div class="pagination">
    @for (int p = 1; p <= ViewBag.TotalPages; p++)
    {
        <a class="@(p == ViewBag.CurrentPage ? "current" : "")" href="@Url.Action("Products", "Home", new { page = p })">@p</a>
    }
</div>

【问题讨论】:

    标签: c# asp.net asp.net-mvc asp.net-mvc-3 asp.net-mvc-4


    【解决方案1】:

    就是因为这行代码……

    ViewBag.TotalPages = Math.Ceiling((double)db.tbl_product.Count() / pageSize);
    

    您需要添加与过滤结果相同的“Where”子句。这个“where”子句应该在调用“Count”之前进行

    编辑

    你有这个...

    ViewBag.TotalPages = Math.Ceiling((double)db.tbl_product.Count() / pageSize);
    

    这是获取所有产品的数量。但是您需要根据特定的搜索条件显示所有产品的数量,对吧?因此,当您调用“Count”时,您需要在调用“Count”之前应用过滤表达式。像这样……

    ViewBag.TotalPages = Math.Ceiling((double)db.tbl_product.Where(c => c.ProductName.Contains(searchString)).Count() / pageSize);
    

    顺便说一下,我说的是这个动作方法里面的代码......

    [HttpPost]
    public ActionResult Index(string searchString)
    

    应该可以的

    【讨论】:

    • 我应该使用这个查询 var query = db.tbl_product.Where(c => c.ProductName.Contains(searchString)).OrderBy(c=>c.ProductId).Skip((page- 1)*pageSize).Take(pageSize).ToList();在 Product 方法中的 ViewBag.TotalPages 下方,因为每次我运行程序或搜索特定数据时,它都会根据计算返回所有页面
    • 改代码后的结果还是一样的..你能详细告诉我哪里错了...
    • 我已编辑我的答案以包含更多详细信息。干杯
    • 现在页数显示正确,但是当我搜索特定记录时,它一次显示所有记录而不是 10 条记录,当我单击特定页面的链接时,它会重定向到根据数据库的所有记录显示页面的所有链接..感谢上述建议....
    • 好的,这是另一个问题。关于这个分页问题,​​您根本没有在您的 Index 操作方法中进行任何分页!!! Skip and Take 的调用发生了什么?
    猜你喜欢
    • 2012-06-03
    • 2011-05-10
    • 2019-02-27
    • 2012-12-16
    • 1970-01-01
    • 2010-10-19
    • 2018-03-22
    • 1970-01-01
    • 2011-05-02
    相关资源
    最近更新 更多