我在这个问题上遇到了一个小问题
我目前有serach和分页工作,但我不知道代码的分页方面是什么。
我只需要分页,不需要搜索,因为我已经拥有它。这是我用于分页和搜索的教程,但不知道要为分页提取什么:
http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application
这是我的分页和搜索控制器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;
using Games.Models;
namespace Games.Controllers
{
public class ShowAllGamesController : Controller
{
//
// GET: /ShowAllGames/
public ActionResult Index(string Ordering, string WordFilter, string DisplaySearchResults, int? CounterForPage)
{
using (var db = new gamezoneDBEntities())
{
ViewBag.Message = TempData["message"];
ViewBag.CurrentSort = Ordering;
ViewBag.NameSortParm = String.IsNullOrEmpty(Ordering) ? "GameName" : "";
ViewBag.DateSortParm = Ordering == "ReleaseYearOfGame" ? "DiscriptionOfGame" : "Date";
{
TempData["DisplaySearchResult"] = DisplaySearchResults;
{
ViewBag.search = DisplaySearchResults;
}
if (Request.HttpMethod == "GET")
{
DisplaySearchResults = WordFilter;
}
else if (DisplaySearchResults == "")
{
ViewData["MyMessage"] = "Nothing Has Been Entered.";
}
else
{
CounterForPage = 1;
}
ViewBag.CurrentFilter = DisplaySearchResults;
var FullDatabaseItem = from b in db.tblGames
select b;
if (!String.IsNullOrEmpty(DisplaySearchResults))
{
FullDatabaseItem = FullDatabaseItem.Where(b => b.GameName.ToUpper().Contains(DisplaySearchResults.ToUpper()));
}
switch (Ordering)
{
case "HeadlineName":
FullDatabaseItem = FullDatabaseItem.OrderBy(b => b.GameName);
break;
case "DatePosted":
FullDatabaseItem = FullDatabaseItem.OrderBy(b => b.ReleaseYear);
break;
case "DiscriptionDate":
FullDatabaseItem = FullDatabaseItem.OrderBy(b => b.ReleaseYear);
break;
default:
FullDatabaseItem = FullDatabaseItem.OrderByDescending(b => b.ReleaseYear);
break;
}
int pageSize = 3;
int pageNumber = (CounterForPage ?? 1);
var PageNumberResults = FullDatabaseItem.ToPagedList(pageNumber, pageSize);
ViewBag.PageNumberResults = FullDatabaseItem.Count();
if (PageNumberResults.Any())
{
return View(PageNumberResults);
}
return View("ErrorView");
}
}
}
public ActionResult AutoCompleteGames()
{
var db = new gamezoneDBEntities();
string term = this.Request.Params["term"].ToString();
return Json(db.tblGames.Where(games => games.GameName.StartsWith(term)).Select(games => games.GameName), JsonRequestBehavior.AllowGet);
}
//
// GET: /ShowAllGames/Details/5
public ViewResult Details(int id)
{
using (var db = new gamezoneDBEntities())
{
tblGame tblgame = db.tblGames.Find(id);
return View(tblgame);
}
}
//
// GET: /ShowAllGames/Create
public ActionResult Create()
{
return View();
}
//
// POST: /ShowAllGames/Create
[HttpPost]
public ActionResult Create(FormCollection collection)
{
try
{
// TODO: Add insert logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
//
// GET: /ShowAllGames/Edit/5
public ActionResult Edit(int id)
{
return View();
}
//
// POST: /ShowAllGames/Edit/5
[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
try
{
// TODO: Add update logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
//
// GET: /ShowAllGames/Delete/5
public ActionResult Delete(int id)
{
return View();
}
//
// POST: /ShowAllGames/Delete/5
[HttpPost]
public ActionResult Delete(int id, FormCollection collection)
{
try
{
// TODO: Add delete logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
}
你可以看到两者都运作良好且是正确的。但我不知道我需要什么分页,任何人都可以帮助我,谢谢。
我尝试过的事情:
int pageSize = 3;
int pageNumber = (CounterForPage ?? 1);
var PageNumberResults = FullDatabaseItem.ToPagedList(pageNumber, pageSize);
ViewBag.PageNumberResults = FullDatabaseItem.Count();
if (PageNumberResults.Any())
{
return View(PageNumberResults);
}
不起作用
谢谢
编辑:
这就是我想要的分页
public ViewResult Index()
{
var Info = db.tblGames.Include(x => x.tblConsole).Where(UserInfo => UserInfo.UserName.Equals(User.Identity.Name)).ToList();
return View(Info);
}
this is my ViewPersonalGames controller
以下是我的分页,如果您将其恢复为IEnumerable
EDIT:
@*<div class="PageCounter">
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber)
of @Model.PageCount
@if (Model.HasPreviousPage)
{
@Html.ActionLink("<<", "Index", new { CounterForPage = 1, Ordering = ViewBag.CurrentSort, WordFilter = ViewBag.WordFilter })
@Html.Raw(" ");
@Html.ActionLink("< Previous Page", "Index", new { CounterForPage = Model.PageNumber - 1, Ordering = ViewBag.CurrentSort, WordFilter = ViewBag.WordFilter })
}
else
{
@:<<
@Html.Raw(" ");
@:< Prev
}
@if (Model.HasNextPage)
{
@Html.ActionLink("Next Page >", "Index", new { CounterForPage = Model.PageNumber + 1, Ordering = ViewBag.CurrentSort, WordFilter = ViewBag.CurrentFilter })
@Html.Raw(" ");
@Html.ActionLink(">>", "Index", new { CounterForPage = Model.PageCount, Ordering = ViewBag.CurrentSort, WordFilter = ViewBag.CurrentFilter })
}
else
{
@:Next>
@Html.Raw(" ")
@:>>
}
* @
答案 0 :(得分:1)
public ViewResult Index()
{
var pageSize = 3;
var pageNumber = (CounterForPage ?? 1);
var Info = db.tblGames
.Include(x => x.tblConsole)
.Where(UserInfo => UserInfo.UserName.Equals(User.Identity.Name))
.OrderBy(UserInfo => UserInfo.UserName)
.Skip(pageSize * (pageNumber - 1)) // skip first 0, 3, 6, 9, 12, etc, rows
.Take(pageSize) // give me only this many rows from the db
.ToList();
return View(Info);
}