2016-02-21 91 views
0

我想實現MVC分頁索引行動的工作。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); 
     } 
    } 

現在的查看

@Html.PagedListPager(Model, page => Url.Action("Index", new {page, pagesize = 5 })) 

,因爲我使用

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

上只有我裝修我的視圖模型與IPagedList接受現在

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

我ViewModdel 的看法是不接受客戶

@{ 
      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 })) 

上查看試圖在形式標籤寫入這個問題,以及從側面表單標籤。

回答

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 })) 
+0

它不是working.Have編輯我的代碼。 – Dave

+0

您需要用您嘗試過的新代碼編輯您的問題,而不是編輯我的答案(您的編輯已被拒絕) –

+0

已添加編輯的代碼。 – Dave