[英]Sorting and Paging with MVC 5 ADO.NET
我有以下代码:
模型
public int ManufactureID { get; set; } public string Manufacture { get; set; } public string ManufactureDescription { get; set; } public bool IsActive { get; set; }
控制者
public ActionResult ManufactureIndex(string manufacture = "") { ManufactureRepository ManufactureRepo = new ManufactureRepository(); ModelState.Clear(); ViewBag.sManufacture = manufacture; return View(ManufactureRepo.ManufactureGetAll(manufacture)); }
资料库
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; }
存放程序
SELECT * FROM _Manufacture WHERE Manufacture = ISNULL(@Manufacture,Manufacture) ORDER BY Manufacture
我的问题是,我想使用sql连接在视图上提供排序和分页。 我怎样才能做到这一点? 请指教。
谢谢。
int pageSize = 10;
int skip = 0;
int recordsTotal = 0;
其中pagesize是要在网格中显示的记录数,其中skip是选择记录的起点, recordsTotal在网格中的记录总数。
recordsTotal = EmpList.Count();
EmpList= EmpList.Skip(skip).Take(pageSize).ToList();
您需要传递页码以及每个请求要获取的记录数和数量。
@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
所以现在当您调用此存储过程时,请传递PageNumber和Records参数值
当您为PageNumber传递1并为记录传递10时,StartingPoint将为1,EndingPoint将为10,因此您将获得10条记录。对于第2页,StartingPoint将为11,EndingPoint将为20,因此您将获得10条记录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.