asp.net MVC: PagedList + View Model

To pass view model with PagedList:


1. Controller action must use HttpGet and use View Model as action parameter

public ActionResult Index(UserIndexModel model)

2. In the action return the view model with PagingMetaData

model.PagingMetaData = new StaticPagedList<UserIndexItem>(model.Users, model.Page.Value, DEFAULT_PAGE_SIZE, total);

return model;

PagingMetaData is defined on View Model like below:

public IPagedList PagingMetaData { get; set; }


3. In View use the Pager like below to pass data needed back to action by view model:

@Html.PagedListPager(new StaticPagedList<Report.Areas.TIS.Models.UserIndexItem>(Model.Users, Model.PagingMetaData),

page => Url.Action("Index", new RouteValueDictionary() {

{ "Page", page },

{ "FilterName", Model.FilterName },

{ "FilterDateBegin", Model.FilterDateBegin },

{ "FilterDateEnd", Model.FilterDateEnd },

{ "FilterSiteId", Model.FilterSiteId },

{ "FilterPosition", Model.FilterPosition},

{ "FilterUpdated",false} }),

PagedListRenderOptions.Classic)

参考:

http://stackoverflow.com/questions/14258212/mvc-posting-ipagedlist

http://czetsuya-tech.blogspot.co.id/2011/05/mvc3-dynamic-search-paging-using.html#.VkHD8TahfIU

https://github.com/TroyGoode/PagedList

你可能感兴趣的:(asp.net MVC: PagedList + View Model)