gilesrpa
gilesrpa

Reputation: 1061

MVC viewModel and paged results

I have a need to pass into a controller a viewmodel as per below.

[HttpPost]
public JsonResult GetSearchResultsJson(SearchCriteria searchCriteria) 
{
}

SearchCriteria is defined as:

public class SearchCriteria
{
    public SearchData searchData { get; set; }
    public SearchMode searchMode { get; set; }
}

Where SearchMode is:

public class Searchmode
{
    public int? mode { get; set; }
    public int? pageNumber { get; set; }
    public int? pageSize { get; set; }
}

And SearchData has 61 properties that define what items are to search for.

public class SearchData
{
  public string name {get;set;}
  ....
  public int age {get;set;}
}

I populate an object using jQuery and pass that to the controller. .Net converts this object into an object of type SearchCriteria. All is working, but when the PagedListPager control is rendered, how do i emulate the jQuery used to create the object?

At the moment I have the following code:

@Html.PagedListPager(Model.DocumentsPaged, pageNumber => Url.Action("GetSearchResultsJson", XXXXXXXXXXXXXXXXXXXXXXX),pLRO)

And do not know what to put in the bit marked as XXXXXXXXXXXXXXXXXXXXXXX.

Within jQuery, I can modify the pageNumber property of the SerachMode object and this does provide me with the correct page, but it is precisely this property that I need to update within the Html.PagedListPager helper.

Upvotes: 1

Views: 254

Answers (2)

Peter
Peter

Reputation: 684

Your method GetSearchResultsJson(SearchCriteria searchCriteria) is marked [HttpPost], so you can leave the URL parameters blank and just use Url.Action("GetSearchResultsJson"). The search parameters for a POST go in the body of the request instead of the URL.

Because the Html.PagedListPager method expects a 'page' parameter (assuming you are using the method from the PagedList.Mvc NuGet package), you may want to write your search function like this:

GetSearchResultsJson(int page, int SearchCriteria searchCriteria) 

Upvotes: 0

florianbaer
florianbaer

Reputation: 170

As described in example here you can pass the page.

I really suggest you to clone the example code and play a bit with it. Probably you have to add your search parameters as well, in case you lose them server-side.

Upvotes: 2

Related Questions