Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在ASP.net MVC中实现分页时的问题_C#_Asp.net_Asp.net Mvc_Asp.net Mvc 3_Asp.net Mvc 4 - Fatal编程技术网

C# 在ASP.net MVC中实现分页时的问题

C# 在ASP.net MVC中实现分页时的问题,c#,asp.net,asp.net-mvc,asp.net-mvc-3,asp.net-mvc-4,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 sr

在我的上一个问题中,我询问了在实现分页以及根据找到的记录数搜索和更新页面数时的相关问题。我是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());
}
    }
}
@model IEnumerable
指数
$(文档).ready(函数(){
$('#按钮1')。单击(函数(){
$.ajax({
键入:“POST”,
contentType:“应用程序/json;字符集=utf-8”,
url:“主页/索引”,
数据:“{'searchString':'”+document.getElementById('searchString')。value+“}”,
async:false,
成功:功能(响应){
$('#showData').html(响应)
},
错误:函数(){alert(“error”);}
});
});
});
@文本框(“搜索字符串”)
@{Html.RenderPartial(“ViewProduct”);}
HomeController.cs
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Mvc;
使用SearchRecord.Models;
名称空间SearchRecord.Controllers
{
公共类HomeController:控制器
{
//
//回家/
AdventureWorkEntities db=新AdventureWorkEntities();
常量int pageSize=10;
[HttpGet]
公共行动结果产品(int page=1)
{
var products=db.tbl_product.OrderBy(p=>p.ProductId).Skip((第1页)*pageSize.Take(pageSize).ToList();
ViewBag.CurrentPage=第页;
ViewBag.PageSize=页面大小;
ViewBag.TotalPages=数学上限((双精度)db.tbl_product.Count()/pageSize);
返回视图(产品);
}
[HttpPost]
公共操作结果索引(字符串搜索字符串)
{
int page=1;
var query=db.tbl\u product.OrderBy(p=>p.ProductName).Skip((第1页)*pageSize.Take(pageSize).ToList()。其中(p=>p.ProductName.Contains(searchString));
ViewBag.TotalPages=Math.天花((双精度)db.tbl_product.Where(c=>c.ProductName.Contains(searchString)).Count()/pageSize);
返回PartialView(“Viewproduct”,query.ToList());
}
}
}
ViewProduct.cshtml(局部视图)

@model IEnumerable
@{int i=1;}
@foreach(模型中的var p)
{
@p、 产品ID
@p、 产品名称
}

@对于(int p=1;p它是因为这行代码

ViewBag.TotalPages = Math.Ceiling((double)db.tbl_product.Count() / pageSize);
您需要添加用于筛选结果的同一“Where”子句。此“Where”子句应位于调用“Count”之前

编辑 你有这个

ViewBag.TotalPages = Math.Ceiling((double)db.tbl_product.Count() / pageSize);
这是获取所有产品的计数。但是您需要根据特定的搜索条件显示所有产品的计数,对吗?因此,当您调用“计数”时,您需要在调用“计数”之前应用过滤表达式。就像这样

ViewBag.TotalPages = Math.Ceiling((double)db.tbl_product.Where(c => c.ProductName.Contains(searchString)).Count() / pageSize);
顺便说一句,我说的是这个动作方法中的代码

[HttpPost]
public ActionResult Index(string searchString)

这应该行得通,因为这行代码

ViewBag.TotalPages = Math.Ceiling((double)db.tbl_product.Count() / pageSize);
您需要添加用于筛选结果的同一“Where”子句。此“Where”子句应位于调用“Count”之前

编辑 你有这个

ViewBag.TotalPages = Math.Ceiling((double)db.tbl_product.Count() / pageSize);
这是获取所有产品的计数。但是您需要根据特定的搜索条件显示所有产品的计数,对吗?因此,当您调用“计数”时,您需要在调用“计数”之前应用过滤表达式。就像这样

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).跳过((第1页)*pageSize.Take(pageSize).ToList();位于Product方法中的ViewBag.TotalPages下方,因为每次运行程序或搜索特定数据时,它都会返回基于计算的所有页面。更改代码后结果相同。您能详细告诉我哪里错了吗…我已编辑了答案以包含更多详细信息。谢谢,显示了页数d正确,但当我搜索特定记录时,它会显示所有记录,而不是一次显示10条记录,当我单击特定页面的链接时,它会重定向以显示基于数据库所有记录的页面的所有链接。感谢上述建议…好的,这是另一个问题。关于此分页问题,您您的索引操作方法中根本没有进行任何分页!!!要跳过和执行的调用发生了什么?我是否应该使用此查询var query=db.tbl\u product.Where(c=>c.ProductName.Contains(searchString)).OrderBy(c=>c.ProductId).Skip((page-1)*pageSize.Take(pageSize).ToList();位于Product方法中的ViewBag.TotalPages下方,因为每次运行程序或搜索特定数据时,它都会返回基于计算的所有页面。更改代码后结果相同。您能详细告诉我哪里错了吗…我已编辑了答案以包含更多详细信息。谢谢,显示了页数d正确,但当我搜索特定记录时,它会显示所有记录,而不是一次显示10条记录,当我单击特定页面的链接时,它会重定向以显示基于数据库所有记录的页面的所有链接。感谢上述建议…好的,这是另一个问题。关于此分页问题,您您的索引操作方法中根本没有进行任何分页!!!要跳过和执行的调用发生了什么?