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

时间:2018-02-15 00:43:16

标签: c# asp.net-mvc ado.net


我有以下代码:

  1. 模型

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

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

    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. 存储过程

    SELECT      *
    FROM        _Manufacture
    WHERE       Manufacture = ISNULL(@Manufacture,Manufacture)
    ORDER BY    Manufacture
    
  5. 我的问题是,我想使用sql连接在视图上提供排序和分页。我怎样才能做到这一点?请指教。

    谢谢。

3 个答案:

答案 0 :(得分:0)

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

pagesize 是您要在网格中显示的记录数, 跳过是选择记录的起点, recordsTotal 网格中的记录总数。

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

答案 1 :(得分:0)

您需要传递您要为每个请求获取的页码和号码以及记录数。

@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条记录类似于Page 2 StartingPoint将是11而EndingPoint将是20,因此你将获得10条记录。

答案 2 :(得分:0)

谢谢你的回答。我有自己的答案。我正在按照下面的教程创建排序和分页。

Sorting, Filtering, and Paging with the Entity Framework in an ASP.NET MVC Application

真的很感激。
谢谢。