我想弄清楚在asp.net MVC中用ViewModel實現pagedlist的正確方法。PagedList ViewModel實現
說我有以下PagedClientViewModel:
public class PagedClientViewModel
{
public PagedList.IPagedList<ClientViewModel> Clients { get; set; }
}
public class ClientViewModel
{
public string ClientNumber { get; set; }
public string ClientName { get; set; }
}
我的觀點將參考模型如下:
@model PagedClientViewModel
和操作方法看起來是這樣的:
public ActionResult Index(int? page)
{
var pageNumber = page ?? 1;
var clients = GetAllClients();
var onePageOfClients = clients.ToPagedList(pageNumber, 25);
PagedClientViewModel model = new PagedClientViewModel();
var clientViewModels = new List<ClientViewModel>();
foreach (var client in clients)
{
ClientViewModel clientVM = new ClientViewModel
{
ClientName = client.CLIENTNAME,
ClientNumber = client.CLIENTNO,
};
clientViewModels.Add(clientVM);
}
model.Clients = //how do I add the clientViewModels to the PagedList<ClientViewModel>?
return View(model);
}
我不想在創建th時從數據庫迭代整個客戶端記錄列表e viewmodels - 我通過一個包含pagedlist的視圖模型來過度複雜化事物嗎?我不想使用ViewBag!
我的ViewModel應該是什麼樣子?