在ASP.net MVC中实现Paging时出现的问题

时间:2013-12-15 06:28:50

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

在我之前的问题中,我根据发现的记录数量询问了实现分页和搜索以及更新页面数量时的相关问题。我是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>

1 个答案:

答案 0 :(得分: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)

那应该有用