我正在尝试返回带分页的类别页面....我在我的视图中遇到了问题,我无法处理View Model Members,也无法控制它们......
这是我的ViewModel:
public class CategoryVm
{
public List<Book> Book { set; get; }
public Category Category_Content { get; set; }
}
此处还有控制器:
public ActionResult Category(int? id, int? page)
{
int pageSize = 6;
int pageNumber = page ?? 1;
if (id != null)
{
var vm = new CategoryVm();
vm.Category_Content = db.Categories.Where(t => t.Category_id == id).Single();
vm.Book = db.Books.Where(b => b.Category_id == id).ToList();
if (vm != null)
{
//Here I have the problem , it telling me that 'CategoryVm' does not contain a definition for 'ToPagedList'
return View(vm.ToPagedList(pageNumber, pageSize));
}
return HttpNotFound();
}
return RedirectToAction("index",new { Controller = "Home" });
}
这是我的观点:
@using PagedList.Mvc
@model PagedList.IPagedList<SmartBookLibrary.ViewModel.CategoryVm>
@{
ViewBag.Title = Model.Category_Content.Category_name+ " - Smart Books Library";
}
<div class="container">
<div class="row">
<div class="col-md-3">
<p class="lead">Categories</p>
<div class="list-group">
<a href="#" class="list-group-item">Developers</a>
</div>
</div>
<div class="col-md-9">
<div class="row">
@foreach (var item in Model.Book)
{
<div class="col-sm-4 col-lg-4 col-md-4">
<figure class="snip1091 navy">
<img src="~/Images/@item.Book_Image" alt="sq-sample6" />
<figcaption>
<label>@item.Book_name</label>
</figcaption><a href="/Category/@item.Category.Category_id"></a>
</figure>
</div>
}
</div>
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
@Html.PagedListPager(Model, page => Url.Action("Category", new {page}))
</div>
</div>
</div>
我认为我无法访问该模型:
ViewBag.Title = Model。 Category_Content
@foreach(模型中的var项目。预订)
那么如何解决这个问题,并使分页工作?
答案 0 :(得分:3)
更新您的视图模型以使用分页列表
public class CategoryVm {
public IPagedList<Book> Book { set; get; }
public Category Category_Content { get; set; }
}
然后将分页列表分配给视图模型
public ActionResult Category(int? id, int? page) {
int pageSize = 6;
int pageNumber = page ?? 1;
if (id != null) {
var vm = new CategoryVm();
vm.Category_Content = db.Categories.Where(t => t.Category_id == id).Single();
var books = db.Books.Where(b => b.Category_id == id).ToList();
if (vm != null) {
//Apply paged list to view model.
vm.Books = books.ToPagedList(pageNumber, pageSize);
return View(vm);
}
return HttpNotFound();
}
return RedirectToAction("index",new { Controller = "Home" });
}
现在在您的视图中更改模型。
@model SmartBookLibrary.ViewModel.CategoryVm
并且
Page @(Model.Book.PageCount < Model.Book.PageNumber ? 0 : Model.Book.PageNumber) of @Model.Book.PageCount
@Html.PagedListPager(Model.Book, page => Url.Action("Category", new {page}))
答案 1 :(得分:1)
根据这里ToPageList将应用于IEnumerable或IQueryable: https://github.com/troygoode/PagedList
var vm = new CategoryVm();
vm.Category_Content = db.Categories.Where(t => t.Category_id == id).Single();
vm.Book = db.Books.Where(b => b.Category_id == id).ToList();
if (vm != null)
{
var newListCategoryVm = new List<CategoryVm>() {vm}
return View(newListCategoryVm.ToPagedList(pageNumber, pageSize));
}
在视图中,您现在知道您需要为每个或任何您喜欢的内容。