How can I set values to PagerParameter parameter in Controller action

Topics: Core, Customizing Orchard, General
Aug 20, 2013 at 2:13 PM
Hi,
I am trying to send PagerParameter property values as JSON to the controller action. But both the properties are ending up as Null.

public ActionResult DisplayPaged(string subjectPath, PagerParameters pagerParameters)
{
..
}

This is how I am sending the values from my AJAX method in the View:

$.ajax({
url: "@Url.Action(actionName: "DisplayPaged", controllerName: "Question")",
data: { 'subjectPath': 'lending', pagerParameters: { 'Page': pageCounter, 'PageSize': 2 } },
...
...
});


But both pagerParameter.Page and pagerParameter.PageSize properties are null.

Please help!
Developer
Aug 20, 2013 at 4:13 PM
Edited Aug 20, 2013 at 4:14 PM
First, enclose pagerParameters in quotes.

Have you set up contentType to "application/json" (Content-Type header) and dataType to "json"?
Have you checked if correct JSON data is being sent (eg. with Fiddler)?
Also you may need to enclose the data object in double quotes.

Correct request should look like this:
$.ajax({ 
    url: "@Url.Action(actionName: "DisplayPaged", controllerName: "Question")", 
    data: "{ 'subjectPath': 'lending', 'pagerParameters': { 'Page': " + pageCounter + ", 'PageSize': 2 } }", 
    contentType: "application/json; charset=utf-8",
    dataType: "json"
...
}); 
Aug 20, 2013 at 5:47 PM
pszmyd wrote:
First, enclose pagerParameters in quotes.

Have you set up contentType to "application/json" (Content-Type header) and dataType to "json"?
Have you checked if correct JSON data is being sent (eg. with Fiddler)?
Also you may need to enclose the data object in double quotes.

Correct request should look like this:
$.ajax({ 
    url: "@Url.Action(actionName: "DisplayPaged", controllerName: "Question")", 
    data: "{ 'subjectPath': 'lending', 'pagerParameters': { 'Page': " + pageCounter + ", 'PageSize': 2 } }", 
    contentType: "application/json; charset=utf-8",
    dataType: "json"
...
}); 
I made all suggested changes but still property values are Null. Also after wrapping the data within double quote, subjectPath parameter is becoming Null. I then removed the double quote and subjectPath parameter is getting value "lending", but as usual PageParameter properties are not being set.
Developer
Aug 20, 2013 at 6:21 PM
Edited Aug 20, 2013 at 6:23 PM
Actually, model binding from JSON won't work with multiple parameters in ASP.NET MVC by default. You need to wrap both parameters in a single model object and use it as the only action parameter, eg.
public class MyModel {
    public string SubjectPath {get;set;}
    public PagerParameters PagerParameters {get;set;}
}
public ActionResult DisplayPaged(MyModel model) { ... } 
Aug 20, 2013 at 7:56 PM
pszmyd wrote:
Actually, model binding from JSON won't work with multiple parameters in ASP.NET MVC by default. You need to wrap both parameters in a single model object and use it as the only action parameter, eg.
public class MyModel {
    public string SubjectPath {get;set;}
    public PagerParameters PagerParameters {get;set;}
}
public ActionResult DisplayPaged(MyModel model) { ... } 
Ah! Thanks a lot for the valuable tip!
Coordinator
Aug 22, 2013 at 5:15 AM
When you cross-post, please give a link between your posts, otherwise you're wasting everybody's time...