简体   繁体   中英

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. 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.

  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

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.

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

Really appreciated.
Thank you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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