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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章