我一直在尝试使用PagedList包来获取索引视图的分页.一切进展顺利,在控制器级别一切正常,每页只显示5条记录,并根据查询字符串显示相应的页面.
我的问题是在视图中.我将@Model更改为PagedList.IPagedList
可以访问Model.HasNextPage
其他属性,但现在@Html.DisplayNameFor(model => model.ItemName)
已经不再有效了.我收到此错误:
PagedList.IPagedList<Dossier.Models.Item>' does not contain a definition for 'ItemName' and no extension method 'ItemName' accepting a first argument of type 'PagedList.IPagedList<Dossier.Models.Item>' could be found (are you missing a using directive or an assembly reference?)
以下是视图的相关部分:
@model PagedList.IPagedList<Dossier.Models.Item>
@using Dossier.Models.Item
...
<th>
@Html.DisplayNameFor(model => model.ItemName)
</th>
Run Code Online (Sandbox Code Playgroud)
似乎IPagedList与DisplayNameFor()不兼容.知道为什么会这样,以及我如何解决它?我知道我可以手动输入列名,但我希望以后可以在模型中保留(并且可以更改)该信息.
我正在尝试在我的ASP.Net应用程序中使用PagedList,我在微软网站上找到了这个例子http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/sorting -filtering-和寻呼与-的实体框架功能于一个-ASP净MVC-应用
在使用ViewModel的复杂情况下如何使用PagedList?我正在尝试添加一个没有成功的PagedList到这里发布的教师示例:http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/reading-related-data-with -the-实体框架功能于一个-ASP净MVC-应用
问题是ViewModel由类而不是简单字段组成,因此我无法使用ToPageList()方法转换结果.
这是ViewModel结构:
using System.Collections.Generic;
using ContosoUniversity.Models;
namespace ContosoUniversity.ViewModels
{
public class InstructorIndexData
{
public IEnumerable<Instructor> Instructors { get; set; }
public IEnumerable<Course> Courses { get; set; }
public IEnumerable<Enrollment> Enrollments { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
我需要将三个表连接到ViewModel并在View中显示结果.
如果我不使用部分页面,PagedList.Mvc工作正常,但是当我使用带有ajax的部分页面加载网格时,分页存在问题.我在TroyGoode的支持下结束了https://github.com/TroyGoode/ PagedList/issues/26#issuecomment-6471793,但是为支持提供的链接不起作用.现在,我已经这样使用了
@Html.PagedListPager((IPagedList)Model.MovieInforamtions, page => Url.Action("GetMovieDatabase", new { page }))
Run Code Online (Sandbox Code Playgroud)
,它加载页面,但我需要ajaxically更改分页.我怎样才能做到这一点?
我正在尝试显示基于类别过滤器和ItemsPerPage的过滤产品列表,但在尝试将其与PagedList一起使用时遇到了一些问题.
如果我需要编写自己的分页代码,或者有办法使用PagedList获得我需要的结果,那么具有PagedList专业知识的人可以建议我.
我正在使用LINQ的Skip&Take函数来获取当前页面上需要显示的行数,但我仍然希望根据过滤器的总计数来分页链接显示页面.
例如:我的搜索过滤器找到50个结果,但由于我每页的行数是10个项目,我使用LINQ的Skip()和Take()只返回10行.我仍然需要在我的View.cshtml中显示页面链接<< 1 | 2 | 3 | 4 | 5 >> 现在使用默认的PagedList,我只得到<< 1 >>,我知道为什么我只看到一个页面,但我只想知道如何使其工作以显示正确数量的页面链接,而只获得一个子集结果
**我的目标是将优化的查询写入数据库,以便网页响应性能快速.
这是我的Action方法的代码.代码获得了正确的结果,但分页不起作用,因为我需要它:
public ViewResult List(int page =1, string category =null)
{
if (category != null) this.CurrentCategory = category;
var products = repository.Products
.Where(p => this.CurrentCategory == null || p.Category == this.CurrentCategory)
.OrderBy(p => p.ProductID)
.Skip((page -1) * PageSize)
.Take(PageSize);
return View(products.ToList().ToPagedList(page, PageSize));
}
Run Code Online (Sandbox Code Playgroud)
以下是View中处理分页的代码段.我查看了项目的Github网站,但找不到提供自定义页面的扩展方法.我认为它只会根据"每页的项目数"和@Model中产品的Count()来呈现页数:
@model IPagedList<Product>
//foreach loop that renders the @Model
//Code that displays the pagination using PagedList …
Run Code Online (Sandbox Code Playgroud) 我已将以下nuget包添加到我的MVC 5应用程序X.PagedList.Mvc中
我在控制器/视图中返回结果如下:
// Repo
public IPagedList<Post> GetPagedPosts(int pageNumber, int pageSize)
{
var posts = _context.Post
.Include(x => x.Category)
.Include(x => x.Type);
// Return a paged list
return posts.ToPagedList(pageNumber, pageSize);
}
// View model
public class PostViewModel
{
public IPagedList<Post> Posts { get; set; }
...
}
// Controller method
public ActionResult Index(int? page)
{
int pageNumber = page ?? 1;
int pagesize = 5;
var posts = _PostRepository.GetPagedPosts(pageNumber, pagesize);
var viewModel = new PostViewModel
{
Posts = posts,
... …
Run Code Online (Sandbox Code Playgroud) 我正在使用http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with-the中的示例实现一个简单的分页列表索引-entity框架式-AN-ASP净MVC应用程序
我的问题是,当我转到第二页时,搜索字符串"丢失",因此我显示了所有记录,而不是过滤的结果集.
我的index.cshtml:
@using (Html.BeginForm("Index", "", FormMethod.Get))
{
<p>
@Html.TextBox("searchString", ViewBag.currentFilter as string, new { @placeholder = "Search by title or author" })
<input type="submit" value="Search" />
</p>
}
@if (Model.PageCount > 1)
{
@Html.PagedListPager( Model, page => Url.Action("Index", new { page }) )
}
Run Code Online (Sandbox Code Playgroud)
我的控制器:
public ViewResult Index(string sortOrder, string currentFilter, string searchString, int? page)
{
ViewBag.TitleSortParm = sortOrder == "Title" ? "Title desc" : "Title";
ViewBag.AuthorSortParm = sortOrder == "Author" ? "Author desc" : "Author"; …
Run Code Online (Sandbox Code Playgroud) 这是完整的错误消息:方法'Skip'仅支持LINQ to Entities中的排序输入.必须在方法'Skip'之前调用'OrderBy'方法
在"PurchaseOrderController"中,我已将此代码添加到索引方法中:
// GET: PurchaseOrder
public ActionResult Index(int? page)
{
return View(db.PurchaseOrders.ToPagedList(page ?? 1, 3));
}
Run Code Online (Sandbox Code Playgroud)
同样在"PurchaseOrders"的索引视图中,我添加了以下代码:
@using PagedList;
@using PagedList.Mvc;
@model IPagedList<PurchaseOrders.Models.PurchaseOrder>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.First().PurchaseRequest_)
</th>
<th>
@Html.DisplayNameFor(model => model.First().Date)
</th>
<th>
@Html.DisplayNameFor(model => model.First().Requestor)
</th>
<th>
@Html.DisplayNameFor(model => model.First().Vendor)
</th>
<th>
@Html.DisplayNameFor(model => model.First().DateOrdered)
</th>
<th>
@Html.DisplayNameFor(model => model.First().ConfirmedWith)
</th>
<th>
@Html.DisplayNameFor(model => model.First().WorkOrder_)
</th>
<th></th>
</tr>
Run Code Online (Sandbox Code Playgroud) 如果有人可以提供以下建议,我将不胜感激:在我的视图中,我显示了项目列表:
@model PagedList.IPagedList<Items>
@using PagedList.Mvc;
@foreach (var item in Model)
{//displaying data}
Run Code Online (Sandbox Code Playgroud)
我的寻呼机看起来像这样:
@Html.PagedListPager(Model, page => Url.Action("Index", new { humanID = ViewBag.HumanID, page = page }),
new PagedListRenderOptions
{
LinkToFirstPageFormat = "<<",
LinkToPreviousPageFormat = "prev",
LinkToNextPageFormat = "next",
LinkToLastPageFormat = ">>",
})
Run Code Online (Sandbox Code Playgroud)
问题是,当我点击下一页时,它返回空白,没有我的_Layout
.我不想一直重装_Layout.有没有办法使用Ajax.ActionLink for pager?所以我可以UpdateTargedId
在局部视野内?
我已经使用Android的分页库(https://developer.android.com/topic/libraries/architecture/paging.html)实现了分页回收视图.它在获取数据和检索后续页面时工作正常.但是,如何过滤PagedList?假设我有一个搜索小部件,我想搜索当前屏幕上的列表.PagedList.filter()
返回a List
并且PagedListAdapter.setList()
不接受a List
.
我在我的视图中使用了PagedList,但我的脚手架控制器是使用这种默认的索引操作生成的:
public async Task<ActionResult> Index()
{
return View(await db.Claimants.ToListAsync());
}
Run Code Online (Sandbox Code Playgroud)
我找不到PagedList的扩展名async
.我的方法必须改为这样的形式:
public ActionResult Index(int? page)
{
var claimants = db.Claimants.OrderBy(b => b.Name);
var notNullPage = page ?? 1;
return View(claimants.ToPagedList(notNullPage, 50));
}
Run Code Online (Sandbox Code Playgroud)
是否有合理的方式使用PagedList和async
?
pagedlist ×10
asp.net-mvc ×7
c# ×7
pagination ×2
.net ×1
android ×1
asp.net ×1
asp.net-ajax ×1
async-await ×1
linq ×1
razor ×1