Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用MVC分页列表在视图上分页_C#_Asp.net Mvc 4 - Fatal编程技术网

C# 使用MVC分页列表在视图上分页

C# 使用MVC分页列表在视图上分页,c#,asp.net-mvc-4,C#,Asp.net Mvc 4,我想实现MVC分页,以便索引操作正常工作 public ActionResult Index(int? page) { using (NorthwindEntities db = new NorthwindEntities()) { CustomersViewModel model = new CustomersViewModel(); //model.Customers = db.Customer

我想实现MVC分页,以便索引操作正常工作

 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);
        }
    }
仅当我使用IPagedList装饰视图模型时才接受

@model PagedList.IPagedList<SingleCRUD.Models.CustomersViewModel>
行动

model.Customers = (PagedList<Customer>)db.Customers.OrderBy(m => m.CustomerID).ToPagedList(page ?? 1, 5);

on View尝试在表单标签中以及表单标签的外侧写下此内容。

您声称的内容有点不清楚
@model PagedList.IPagedList将不起作用,因为您的模型是
CustomersViewModel
,但如果您使用
@model CustomersViewModel
,它将起作用

如果要显示
客户的分页列表
,则需要显示模型属性

public IPagedList<Customer> Customers { get; set; }

您需要使用您尝试过的新代码编辑您的问题,而不是编辑我的答案(您的编辑已被拒绝)您不应该需要显式的to
PagedList
-您遇到了什么错误?请尝试在model
public IPagedList Customers{get;set;}
中创建属性。动作上的错误消失了,但在视图上它保持不变。这没有任何意义。您确定您正在使用
@Html.PagedListPager(Model.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>
        }
        }
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);
@Html.PagedListPager(Model.Customers, page => Url.Action("Index", new { page, pagesize = 5 }))
public IPagedList<Customer> Customers { get; set; }
@Html.PagedListPager(Model.Customers, page => Url.Action("Index", new {page, pagesize = 5 }))