English 中文(简体)
同样,关于MVC(Razor)的饮食
原标题:Again about paging in MVC (Razor)

I ve read a lot of articles and answers about paging in MVC. I ve managed to make a search results with paging (using PagedList) but now I need another thing. I ve got a list of genres and based on a genre I want to browse all the albums for that genre. The url is Home/Browse/1 (id of genre). How can I page this result because it has already a parameter (genreid) ? Thank s

最佳回答

If you really want to understand how to paginate, I would suggest that you write your own custom paging logic instead of relying on someone else s PagedList abstraction.

当你需要预想结果时,你还必须包括将送往数据库的全面查询的2个其他方面:FilteringSorting

原因何在,除非你同时使用同aging合的分类和过滤方法,否则你将无法计算正确的结果。

例如,我要说的是,我有一个100个牢房的桌子,我想显示每页10个牢房。 头10个A-Z级的行将不同于头10个Z-A级的行。 因此,必须先对表格进行分类,然后才能加以分类。

你必须回答过滤问题——你想要从并非特定基因的所有册中抹去。 同样,这意味着,在产生过滤结果之前,必须采用过滤器。

归根结底,你的控制者将不得不打上数据库,以取得结果。 可以通过向数据库发送过滤和分类的问询,使你感到pa,然后只挑选一个子点,向用户展示想象力。 然而,仅仅因为你可以 t,就意味着你应该这样做。 理想的情况是,你要寄送第1号小盘,用过滤器、分类和想象器来回你所需要的网页。

你们应该能够这样做,比如说:

// assuming pageNumber is Nullable<int>, and is passed as action method argument
pageNumber = pageNumber ?? 1
var pageSize = 10;
var pageIndex = pageNumber.HasValue ? pageNumber.Value - 1 : 0;
var results = context.Albums
    .Where(a => a.GenreId == genreId) // filtering
    .OrderBy(a => a.GenreId) // sorting
    .Skip(pageSize * pageIndex) // skip this many rows (pagination start at)
    .Take(pageSize) // then give me this many rows (pagination chunk)
    .ToList() // execute the query against the database
;
return view(results);

Your view would then have a model type like the following:

@model IEnumerable<Album>

我假定,从使用PedList和MVC,你已经知道如何将页数参数发送给控制器/行动方法。 您过滤URL(Home/Browse/1)时,你将过滤参数移至已经采用的行动方法。

如果你也想让用户对结果进行分类,那么你也必须将信息分类为行动方法参数。 以上代码假设数据库中自然顺序(通常采用Id)。

Same接着页数。 如果你想要使用户能够定制的查询(排泄、过滤、页数、页数)中的任何部分,你必须将这些数值作为行动方法的理由。 否则,控制者将不得不使用上文所列举的价值观。

问题回答

造物者。 你可以做你再次要求做的事情:

public class HomeController : Controller
{
  public object Browse(int genreId, int? page)
  {
    // the *page* variable is bound to the querystring value, not the route
    return View(Genres.All().ToPagedList(page ?? 1, 10);
  }
}




相关问题
Using jquery to get a partial view and updating the UI

I need to render a partial view (returned from the controller) to show some customer summary details. This will need to happen when the user clicks on a button. In the the mean time the user can ...

MVC 2 / MVC 3 / MVC 4

MVC 2 我们可以轻松地创造领域。 现在,我的问题涉及nes地区(地区内)。

Asp.Net MVC 2 - Changing the PropertyValueRequired string

Using a resx file in the App_GlobalResources directory, I ve been able to change the default message for the PropertyValueInvalid string of the model validators. But it doesn t work to translate the ...

ASP.NET MVC 3 - What features do you want to see? [closed]

I know a bunch of people that are really enjoying the improvements that ASP.NET MVC 2 made over the first release. I have just started to migrate our MVC 1 project over and so far areas has totally ...

热门标签