C# -- MVC 4.0 分页与页码
当有很多数据要显示的时候,一次显示完可能比较不合实际,我们有时候可以使用分页显示的形式来处理这种情况。
Razor代码:
@model RunInfoMVC.Controllers.Post @{ ViewBag.Title = "Runs"; } @{ int count = ViewData.Model.runSet.Count<RunsDB.Run>(); int pageSize = 20; int pageCounts = (int)Math.Ceiling(count / (double)pageSize); int currPage = ViewBag.CurrPage; } @section featured { <section class="featured"> <h2>Runs:</h2> </section> } @using (Html.BeginForm("Runs")) { <input type="text" name="ownerID" /> <input type="submit" name="query" value="Owner ID" /> <table aria-sort="ascending" border="1" style="border-color:green"> <tr class="tdStyle" style="background-color:gray"> <th>ID</th> <th>Title</th> <th>BranchID</th> <th>MaxMachines</th> <th>StartTime</th> <th>EndTime</th> </tr> @if (pageCounts > 1) { for (int i = pageSize*(currPage - 1); (i < pageSize*currPage)&& (i<=count-1); i++) { var run = ViewData.Model.runSet.ElementAt<RunsDB.Run>(i); <tr class="tdStyle"> <td>@run.RunID</td> <td>@run.Title</td> <td>@run.BranchID</td> <td>@run.MaxMachines</td> <td>@run.StartTime</td> <td>@run.EndTime</td> </tr> } } else{ for (int i = 0; i < count - 1; i++) { var run = ViewData.Model.runSet.ElementAt<RunsDB.Run>(i); <tr> <td>@run.RunID</td> <td>@run.Title</td> <td>@run.BranchID</td> <td>@run.MaxMachines</td> <td>@run.StartTime</td> <td>@run.EndTime</td> </tr> } } </table> <div> <button type="submit" class="mybtn" name="pre" value="@(currPage-1)">Previous</button> @for (int j = 0; j < pageCounts; j++) { <button type="submit" class="mybtn" name="btn" value="@(j+1)">@(j+1)</button> } <button type="submit" class="mybtn" name="next" value="@(currPage+1)">Next</button> </div> }
Action 代码:
public ActionResult Runs() { Post post = new Post(); currOnwerID = 7976; post.currPage = 1; runs = DTContext.GetTable<RunsDB.Run>(); runSet = (from RunsDB.Run run in runs where run.OwnerID == currOnwerID select run).OrderByDescending(x => x.RunID).ToList<RunsDB.Run>(); if (ViewBag.CurrPage == null) { ViewBag.CurrPage = 1; } post.runSet = runSet; return View(post); } [AcceptVerbs(HttpVerbs.Post)] public ActionResult Runs(FormCollection form ) { Post post = new Post(); post.currPage = 1; if (form.GetValue("btn") != null) { post.currPage = int.Parse(form.GetValue("btn").AttemptedValue); } if (form.GetValue("next") != null) { if (int.Parse(form.GetValue("next").AttemptedValue) >= 25) { post.currPage = 25; } else { post.currPage = int.Parse(form.GetValue("next").AttemptedValue); } } if (form.GetValue("pre") != null) { if (int.Parse(form.GetValue("pre").AttemptedValue) <= 1) { post.currPage = 1; } else { post.currPage = int.Parse(form.GetValue("pre").AttemptedValue); } } if (form.GetValue("query") != null && form.GetValue("ownerID").AttemptedValue != "") { int ownerID = int.Parse(form.GetValue("ownerID").AttemptedValue); currOnwerID = ownerID; post.currPage = 1; runs = DTContext.GetTable<RunsDB.Run>(); runSet = ( from RunsDB.Run run in runs where run.OwnerID == currOnwerID select run ).OrderByDescending(x => x.RunID).ToList<RunsDB.Run>(); } ViewBag.CurrPage = post.currPage; post.runSet = runSet; return View(post); }
Action 中变量runSet, currOnwerID 定义如下:
private static System.Collections.Generic.IEnumerable<RunsDB.Run> runSet; private static int currOnwerID ;
做个备份。