我想实现MVC分页,以便在Index Action上工作。
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
装饰我的视图模型时才被接受@model PagedList.IPagedList<SingleCRUD.Models.CustomersViewModel>
现在正在使用
public IEnumerable<Customer> Customers { get; set; }
在我的ViewModdel上 View不接受客户
@{
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>
}
}
更改名称空间后,定制已停止在客户上。 我的视图模型
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; }
}
所以我在视图级别面临问题如何正确修复它。
尝试了这些变化
模型
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);
由于存在转换错误而无法确定其是否正确,因此必须将其显式转换为分页列表。
视图上的运行时错误。
'System.Web.Mvc.HtmlHelper'不包含'PagedListPager'的定义和最佳扩展方法重载'PagedList.Mvc.HtmlHelper.PagedListPager(System.Web.Mvc.HtmlHelper,PagedList.IPagedList,System。 Func)'有一些无效的参数
错误
错误1无法将类型'PagedList.IPagedList'隐式转换为'PagedList.PagedList'。存在显式转换(您是否错过了演员?)
使用
@Html.PagedListPager(Model.Customers, page => Url.Action("Index", new { page, pagesize = 5 }))
on View尝试在表单标记中以及表单标记外写入。
答案 0 :(得分:1)
你有点不清楚你声称的是什么。由于您的模型为@model PagedList.IPagedList<CustomersViewModel>
,CustomersViewModel
将不,但如果您使用@model CustomersViewModel
,它将有效。
如果您想显示Customer
的分页列表,那么您的模型属性需要
public IPagedList<Customer> Customers { get; set; }
并在视图中使用
@Html.PagedListPager(Model.Customers, page => Url.Action("Index", new {page, pagesize = 5 }))