简体   繁体   English

使用MVC 5 ADO.NET进行排序和分页

[英]Sorting and Paging with MVC 5 ADO.NET


I've the following code: 我有以下代码:

  1. Model 模型

     public int ManufactureID { get; set; } public string Manufacture { get; set; } public string ManufactureDescription { get; set; } public bool IsActive { get; set; } 
  2. Controller 控制者

     public ActionResult ManufactureIndex(string manufacture = "") { ManufactureRepository ManufactureRepo = new ManufactureRepository(); ModelState.Clear(); ViewBag.sManufacture = manufacture; return View(ManufactureRepo.ManufactureGetAll(manufacture)); } 
  3. Repository 资料库

     private SqlConnection con; //To Handle connection related activities private void Connection() { con = new SqlConnection(ConfigurationManager.ConnectionStrings["ITPCNMSCon"].ToString()); } //To view employee details with generic list public List<Manufactures> ManufactureGetAll(string manufacture = "") { Connection(); List<Manufactures> EmpList = new List<Manufactures>(); SqlCommand com = new SqlCommand("_spManufactureGet", con) { CommandType = CommandType.StoredProcedure }; if (string.IsNullOrEmpty(manufacture)) { com.Parameters.AddWithValue("@Manufacture", DBNull.Value); } else { com.Parameters.AddWithValue("@Manufacture", manufacture); } SqlDataAdapter da = new SqlDataAdapter(com); DataTable dt = new DataTable(); con.Open(); da.Fill(dt); con.Close(); //Bind EmpModel generic list using LINQ EmpList = (from DataRow dr in dt.Rows select new Manufactures() { ManufactureID = Convert.ToInt32(dr["ManufactureId"]), Manufacture = Convert.ToString(dr["Manufacture"]), ManufactureDescription = Convert.ToString(dr["ManufactureDescription"]), IsActive = Convert.ToBoolean(dr["IsActive"]) }).ToList(); return EmpList; } 
  4. Store Procedure 存放程序

     SELECT * FROM _Manufacture WHERE Manufacture = ISNULL(@Manufacture,Manufacture) ORDER BY Manufacture 

My question is, I want to provide a sorting and paging on the view using sql connection. 我的问题是,我想使用sql连接在视图上提供排序和分页。 How can I do that? 我怎样才能做到这一点? Please advise. 请指教。

Thank you. 谢谢。

 int pageSize = 10;
 int skip = 0;
 int recordsTotal = 0;

Where pagesize is number of records that you want to display in grid, skip is starting point to pick the records, recordsTotal total number of records in grid. 其中pagesize是要在网格中显示的记录数,其中skip是选择记录的起点, recordsTotal在网格中的记录总数。

  recordsTotal = EmpList.Count();
  EmpList= EmpList.Skip(skip).Take(pageSize).ToList();

You need to pass page number and number and number of records you want to fetch per request. 您需要传递页码以及每个请求要获取的记录数和数量。

@PageNumber int
@Records int


Declare @StartingPoint int, @EndingPoint int
Set @StartingPoint = ((@PageNumber -1 ) * @Records)+1
Set @EndingPoint = @PageNumber  * @Records
Select * from (
  Select Dense_Rank() over(order by <PK>) Order,* from <Table>
)TempRecord where [Order]>= @StartingPoint  and [Order]<=@EndingPoint 

So now when you call this store procedure pass PageNumber and Records parameter value 所以现在当您调用此存储过程时,请传递PageNumber和Records参数值

When you pass 1 for PageNumber and 10 for Records StartingPoint will be 1 and EndingPoint will be 10 so you get 10 records Similarly for Page 2 StartingPoint will be 11 and EndingPoint will be 20 thus you will get 10 records. 当您为PageNumber传递1并为记录传递10时,StartingPoint将为1,EndingPoint将为10,因此您将获得10条记录。对于第2页,StartingPoint将为11,EndingPoint将为20,因此您将获得10条记录。

Thank you for all the answer. 感谢您的所有答复。 I have my own answer. 我有自己的答案。 I am following below tutorial to create sorting and paging. 我在下面的教程中创建排序和分页。

Sorting, Filtering, and Paging with the Entity Framework in an ASP.NET MVC Application 在ASP.NET MVC应用程序中使用实体框架进行排序,筛选和分页

Really appreciated. 非常感谢。
Thank you. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM