【问题标题】:Server side pagination with MVC C# and MySql使用 MVC C# 和 MySql 的服务器端分页
【发布时间】:2017-10-30 13:57:44
【问题描述】:


我不明白服务器端分页如何与 C# MVC 中的 MySql 和 Datatable 一起使用。 我在 C# 中创建了一个 Controlled,在其中我建立了与 MySql 数据库的连接(为了做到这一点,我遵循了这个示例):

public ActionResult connectDB()
    {

        const string DB_CONN_STR = "Server=MyServer;Port=MyPort;Uid=MyUid;Database=MyDB;";

        MySqlConnection cn = new MySqlConnection(DB_CONN_STR);

        try
        {

            string sqlCmd = "select * from t_documento";

            MySqlDataAdapter adr = new MySqlDataAdapter(sqlCmd, cn);
            adr.SelectCommand.CommandType = CommandType.Text;
            DataTable dt = new DataTable();
            adr.Fill(dt); //opens and closes the DB connection automatically !! (fetches from pool)

            return Content(JsonConvert.SerializeObject(dt).ToString());
        }
        catch (Exception ex)
        {
            Console.WriteLine("{oops - {0}", ex.Message);
            return Content(ex.ToString());
        }
        finally
        {
            cn.Dispose(); // return connection to pool

        }        
    }

但是,通过这种方式,我检索了存储在该表中的所有记录,但我想通过实现分页来填充数据表(我的数据表的初始化位于 cshtml 页面中)。 我阅读了很多文章,但没有找到一个明确的 MySql DB 示例。 有人能帮助我吗? 谢谢!

【问题讨论】:

标签: c# mysql model-view-controller pagination datatables


【解决方案1】:

试试这个Example的服务器端分页。

/Controllers/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();
    }
}

/Views/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 }) )

【讨论】:

  • 谢谢,但我只对这个特定的实现感兴趣...我需要 Datatables 和 MySql,而不是 Sql Server 或其他类型的数据库...我不知道特定类型是否db 是否相关,但我真的很难检索使用这种组合的示例。
猜你喜欢
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 2015-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多