Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.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
使用MVC#和MySql进行服务器端分页_C#_Mysql_Model View Controller_Pagination_Datatables - Fatal编程技术网

使用MVC#和MySql进行服务器端分页

使用MVC#和MySql进行服务器端分页,c#,mysql,model-view-controller,pagination,datatables,C#,Mysql,Model View Controller,Pagination,Datatables,我不明白服务器端分页如何在C#MVC中与MySql和Datatable一起工作。 我在C#中创建了一个控件,在该控件中我与一个MySql数据库建立了连接(为了实现这一点,我遵循了这个示例): 然而,通过这种方式,我检索存储在该表中的所有记录,但我希望通过实现分页来填充Datatable(我的Datatable的序列化位于cshtml页面中)。 我读了很多文章,但没有找到一个MySql数据库的清晰示例。 有人能帮我吗? 谢谢大家! 试试服务器端分页的方法 /控制器/ProductControlle


我不明白服务器端分页如何在C#MVC中与MySql和Datatable一起工作。 我在C#中创建了一个控件,在该控件中我与一个MySql数据库建立了连接(为了实现这一点,我遵循了这个示例):

然而,通过这种方式,我检索存储在该表中的所有记录,但我希望通过实现分页来填充Datatable(我的Datatable的序列化位于cshtml页面中)。 我读了很多文章,但没有找到一个MySql数据库的清晰示例。 有人能帮我吗? 谢谢大家!

试试服务器端分页的方法

/控制器/ProductController.cs

public class ProductController : Controller
{
    public object Index(int? page)
    {
        var products = MyProductDataSource.FindAllProducts(); //returns IQueryable<Product> representing an unknown number of products. a thousand maybe?

        var pageNumber = page ?? 1; // if no page was specified in the querystring, default to the first page (1)
        var onePageOfProducts = products.ToPagedList(pageNumber, 25); // will only contain 25 products max because of the pageSize

        ViewBag.OnePageOfProducts = onePageOfProducts;
        return View();
    }
}
公共类ProductController:控制器
{
公共对象索引(int?页)
{
var products=MyProductDataSource.FindAllProducts();//返回表示未知数量产品的IQueryable。可能有一千个?
var pageNumber=page±1;//如果查询字符串中未指定页面,则默认为第一页(1)
var onePageOfProducts=products.ToPagedList(pageNumber,25);//由于页面大小,最多只包含25个产品
ViewBag.OnePageOfProducts=产品的一页;
返回视图();
}
}
/视图/Products/Index.cshtml

@{
    ViewBag.Title = "Product Listing"
}
@using PagedList.Mvc; //import this so we get our HTML Helper
@using PagedList; //import this so we can cast our list to IPagedList (only necessary because ViewBag is dynamic)

<!-- import the included stylesheet for some (very basic) default styling -->
<link href="/Content/PagedList.css" rel="stylesheet" type="text/css" />

<!-- loop through each of your products and display it however you want. we're just printing the name here -->
<h2>List of Products</h2>
<ul>
    @foreach(var product in ViewBag.OnePageOfProducts){
        <li>@product.Name</li>
    }
</ul>

<!-- output a paging control that lets the user navigation to the previous page, next page, etc -->
@Html.PagedListPager( (IPagedList)ViewBag.OnePageOfProducts, page => Url.Action("Index", new { page }) )
@{
ViewBag.Title=“产品列表”
}
@使用PagedList.Mvc//导入此文件,以便获得HTML帮助程序
@使用页面列表//导入此项,以便将列表强制转换到IPagedList(仅因ViewBag是动态的而必需)
产品清单
    @foreach(ViewBag中的var产品。产品的一页){
  • @产品名称
  • }
@Html.PagedListPager((IPagedList)ViewBag.OnePageOfProducts,page=>Url.Action(“Index”,new{page}))
谢谢,但我只对这个具体的实现感兴趣。。。我需要数据表和MySql,而不是Sql Server或其他类型的数据库。。。我不知道特定类型的数据库是否相关,但我确实很难检索使用这种组合的示例。只需向控制器添加页码并将其传递给模型和过程。以下是一些关于SQLServer中分页的好参考:和。我相信MySql数据库将非常相似。
@{
    ViewBag.Title = "Product Listing"
}
@using PagedList.Mvc; //import this so we get our HTML Helper
@using PagedList; //import this so we can cast our list to IPagedList (only necessary because ViewBag is dynamic)

<!-- import the included stylesheet for some (very basic) default styling -->
<link href="/Content/PagedList.css" rel="stylesheet" type="text/css" />

<!-- loop through each of your products and display it however you want. we're just printing the name here -->
<h2>List of Products</h2>
<ul>
    @foreach(var product in ViewBag.OnePageOfProducts){
        <li>@product.Name</li>
    }
</ul>

<!-- output a paging control that lets the user navigation to the previous page, next page, etc -->
@Html.PagedListPager( (IPagedList)ViewBag.OnePageOfProducts, page => Url.Action("Index", new { page }) )