使用MVC页面列表在页面上分页 [英] Paging on View with MVC Paged List
问题描述
我想实现MVC分页,以便在索引操作上正常工作.
I wanna implement MVC paging so on the Index Action its working.
public ActionResult Index(int? page)
{
using (NorthwindEntities db = new NorthwindEntities())
{
CustomersViewModel model = new CustomersViewModel();
//model.Customers = db.Customers.OrderBy(m => m.CustomerID).OrderByDescending(m=>m.CustomerID).Take(10).ToList();
model.Customers = db.Customers.OrderBy(m => m.CustomerID).OrderByDescending(m=>m.CustomerID).Take(10).ToList().ToPagedList(page ?? 1,5);
model.SelectedCustomer = null;
var list = new List<int>();
for (int i = 1; i <= 20; i++)
{
list.Add(i);
}
SelectList selectedList = new SelectList(list);
ViewBag.DdList = selectedList;
//model.Countries = db.Countries.ToList();
model.CountryList = new SelectList(BLDDLCountry.GetCountry(), "CountryId", "CountryName");
model.DisplayMode = "WriteOnly";
return View(model);
}
}
现在在视图上
@Html.PagedListPager(Model, page => Url.Action("Index", new {page, pagesize = 5 }))
仅当我用IPagedList装饰我的视图模型时,才会被接受
Is accepted only if i decorate my View Model with IPagedList
@model PagedList.IPagedList<SingleCRUD.Models.CustomersViewModel>
现在使用中
public IEnumerable<Customer> Customers { get; set; }
在我的ViewModdel上 视图不接受客户
On My ViewModdel The View is not accepting the Customers
@{
foreach (var item in Model.Customers)
{
if (Model.SelectedCustomer != null)
{
if (item.CustomerID ==
Model.SelectedCustomer.CustomerID)
{
@:<tr class="SelectedCustomer">
}
else
{
@:<tr>
}
}
else
{
@:<tr>
}
<td>@item.CustomerID</td>
<td>@item.CompanyName</td>
@*<td><input type="submit"
formaction="/home/select/@item.CustomerID"
value="Select" /></td>*@
<td><input type="submit"
formaction="/home/Edit/@item.CustomerID"
value="Edit" /></td>
<td></td>
@:</tr>
}
}
更改名称空间后,Customers上的转到"定义已停止. 我的视图模型
And Go to definition has stopped on Customers after changing the name space. My View Model
public class CustomersViewModel
{
public int CustomerID { get; set; }
public string CompanyName { get; set; }
public string ContactName { get; set; }
public string ContactTitle { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string Region { get; set; }
public Nullable<int> PostalCode { get; set; }
public string Country { get; set; }
public Nullable<int> Phone { get; set; }
public Nullable<int> Fax { get; set; }
public IEnumerable<Customer> Customers { get; set; }
public Customer SelectedCustomer { get; set; }
public string DisplayMode { get; set; }
public List<Country> Countries { get; set; }
public SelectList CountryList { get; set; }
}
所以我在视图级别面临问题,如何正确修复它.
So I am facing issue at the view level how do I correctly fix it.
尝试了这些更改
型号
public PagedList<Customer> Customers { get; set; }
查看
@model SingleCRUD.Models.CustomersViewModel
@using PagedList;
@using PagedList.Mvc;
@Html.PagedListPager(Model.Customers, page => Url.Action("Index", new { page, pagesize = 5 }))
动作
model.Customers = (PagedList<Customer>)db.Customers.OrderBy(m => m.CustomerID).ToPagedList(page ?? 1, 5);
由于存在转换错误(不确定其是否正确),因此必须将其显式转换为分页列表.
Had to explicitly convert it to Paged List as there was a conversion error not sure whether its correct.
视图上的运行时错误.
'System.Web.Mvc.HtmlHelper'不包含'PagedListPager'的定义,最佳扩展方法重载'PagedList.Mvc.HtmlHelper.PagedListPager(System.Web.Mvc.HtmlHelper,PagedList.IPagedList,系统. Func)'有一些无效的参数
'System.Web.Mvc.HtmlHelper' does not contain a definition for 'PagedListPager' and the best extension method overload 'PagedList.Mvc.HtmlHelper.PagedListPager(System.Web.Mvc.HtmlHelper, PagedList.IPagedList, System.Func)' has some invalid arguments
错误
错误1无法将类型'PagedList.IPagedList'隐式转换为'PagedList.PagedList'.存在显式转换(您是否缺少演员表?)
Error 1 Cannot implicitly convert type 'PagedList.IPagedList' to 'PagedList.PagedList'. An explicit conversion exists (are you missing a cast?)
使用
@Html.PagedListPager(Model.Customers, page => Url.Action("Index", new { page, pagesize = 5 }))
在View上尝试将其写在form标签以及form标签之外.
on View tried writing this in the form tag as well as out side the form tag.
推荐答案
不清楚您所声称的内容.由于模型为CustomersViewModel
,所以@model PagedList.IPagedList<CustomersViewModel>
将不起作用,但是如果您使用@model CustomersViewModel
,则将起作用.
Its a bit unclear what you claiming. @model PagedList.IPagedList<CustomersViewModel>
will not work since your model is CustomersViewModel
but it will work if your use @model CustomersViewModel
.
如果要显示Customer
的页面列表,则您的模型属性必须为
If you wanting to display a paged list of Customer
, then your model property needs to be
public IPagedList<Customer> Customers { get; set; }
并在视图中使用
@Html.PagedListPager(Model.Customers, page => Url.Action("Index", new {page, pagesize = 5 }))
这篇关于使用MVC页面列表在页面上分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!