我想在我的项目中添加简单的分页程序。虽然到目前为止我已经创建了一个存储过程:
CREATE PROCEDURE getStudent_Paging
(
@PageIndex INT,
@PageSize INT,
)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @minIndex int,
@maxIndex int
SET @minIndex = (@PageIndex * @PageSize) + 1;
SET @maxIndex = (@PageIndex +1) * @PageSize;
SELECT ROW_NUMBER() OVER (ORDER BY ID ASC)AS RowNumber, * INTO #Results
FROM dbStudent;
SELECT @TotalRows = COUNT(*) FROM #Results;
SELECT * FROM #Results WHERE RowNumber BETWEEN' +
@minIndex + 'AND' + @maxIndex + ';
DROP TABLE #Results
END
我还展示了前 5 条记录。但是在导航按钮中应该做些什么来导航到另一个页面。
我的控制器逻辑:
public ActionResult Index()
{
DatabaseConnString db = new DatabaseConnString();
SqlConnection conn = db.ConnectDB();
List<StudentClass> list = new List<StudentClass>();
SqlCommand myCommand = new SqlCommand("getStudent_Paging", conn);
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@PageIndex", pageIndex);
myCommand.Parameters.AddWithValue("@PageSize", pageSize);
SqlDataReader reader = myCommand.ExecuteReader();
while (reader.Read())
{
int ID = Int32.Parse(reader["ID"].ToString());
string Name = reader["Name"].ToString();
string Address = reader["Address"].ToString();
string PhoneNumber = reader["PhoneNumber"].ToString();
StudentClass model = new StudentClass
{
ID = ID,
Name = Name,
Address = Address,
PhoneNumber = PhoneNumber
};
list.Add(model);
}
return View(list);
}