Reputation: 14540
What do I use for my second parameter to '@Html.PagedListPager'? If I have a action in my controller that accepts a viewmodel like this
[HttpPost]
[AllowAnonymous]
public ActionResult Search(HomePageViewModel viewModel)
{
var pagedList = repository.GetYogaList(viewModel.SearchQuery, viewmodel.Date)
viewModel.YogaList = pagedList;
if (Request.IsAjaxRequest())
{
return PartialView("_mySpaces", viewModel);
}
return View(viewModel);
}
and a partial page containing the paged list html helper
here is the partial '_mySpaces.html'
@model HomePageViewModel
<div id="yogaSpaceList">
<div class="pagedList">
@Html.PagedListPager(Model.YogaSpaces, page => Url.Action("Search", new { Model }), PagedListRenderOptions.MinimalWithItemCountText)
</div>
@foreach (var space in Model.YogaSpaces) {
<div>
<h4>@space.Overview.Title</h4>
<div>
@space.Overview.Summary
</div>
</div>
}
</div>
Upvotes: 3
Views: 4381
Reputation: 835
Ok you can pass a viewmodel back to an action but as previously state it needs to be a GET method so mark your action with the HttpGet attribute. The MVC framework will translate/bind the query string to your view model.
Your controller should look something like this:
[HttpGet]
public ActionResult Search(ViewModel viewModel)
{
\\ do something useful here and return ActionResult .....
}
You will need to add a page property to your ViewModel and a method allows your .cshtml code to set the page number and returns the viewmodel as an object. MVC will translate the object into a query string for the action link.
Public class ViewModel {
// other properties and stuff
public int? Page { get; set; }
public object ToPagedListParameters(int page)
{
this.Page = page;
return this;
}
}
Then all that is need is a small adjustment to your .cshtml file
<div class="pagedList">
@Html.PagedListPager(viewModel, page => Url.Action("Search",viewModel.ToPagedListParameters(page))
</div>
Note: I have only got this working with simple viewModels and have not tried it with viewModels that contain collections or lists.
Upvotes: 2