2012-04-09 39 views
0

我有過它的年齡如何提取尋呼

一個小問題,我已經得到了目前我的檢索算法和分頁的工作,但我不知道代碼的尋呼方面是什麼。

我只需要分頁,不需要搜索,因爲我已經擁有了它。這是我用分頁和檢索算法,但不知道該怎麼只提取分頁教程:

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 
    &nbsp; 
    @if (Model.HasPreviousPage) 
    { 

     @Html.ActionLink("<<", "Index", new { CounterForPage = 1, Ordering = ViewBag.CurrentSort, WordFilter = ViewBag.WordFilter }) 
     @Html.Raw("&nbsp;"); 
     @Html.ActionLink("< Previous Page", "Index", new { CounterForPage = Model.PageNumber - 1, Ordering = ViewBag.CurrentSort, WordFilter = ViewBag.WordFilter }) 
    } 
    else 
    { 
     @:<< 
     @Html.Raw("&nbsp;"); 
     @:< Prev 
    } 
    &nbsp; 
    @if (Model.HasNextPage) 
    { 
     @Html.ActionLink("Next Page >", "Index", new { CounterForPage = Model.PageNumber + 1, Ordering = ViewBag.CurrentSort, WordFilter = ViewBag.CurrentFilter }) 
     @Html.Raw("&nbsp;"); 
     @Html.ActionLink(">>", "Index", new { CounterForPage = Model.PageCount, Ordering = ViewBag.CurrentSort, WordFilter = ViewBag.CurrentFilter }) 
    } 
    else 
    { 
     @:Next> 
     @Html.Raw("&nbsp;") 
     @:>> 
    } 

不再適用* @

+0

請得到更清晰的不是「不工作」。它編譯失敗嗎?運行時失敗?還是它不像預期的那樣行事? – danludwig 2012-04-09 20:34:50

+0

只是不運行,因此它不適合通用 – user1137472 2012-04-09 20:41:15

+0

不運行?所以它構建/編譯,但是當你在瀏覽器窗口中訪問它時,你會看到一個黃色的錯誤頁面(黃色死亡屏幕/ YSOD)?如果是這樣,這是一個運行時失敗,並且如果您發佈拋出的異常,它將有所幫助。 – danludwig 2012-04-09 20:45:20

回答

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); 
} 
+0

這個詞混淆,我得到以下錯誤:只有LINQ to Entities中的排序輸入支持'Skip'方法。方法'OrderBy'必須在方法'Skip'之前調用。 – user1137472 2012-04-09 23:13:35

+0

然後在Skip()之前包含'OrderBy()'方法。看到我編輯的答案。 – danludwig 2012-04-09 23:36:50

+0

將嘗試謝謝 – user1137472 2012-04-09 23:38:26