I have succeded in creating simple objects as follows.
The json on the client side js is as follows.
{"Page":1, "Take":10, "SortOrder":"Asc", "PropName":"Id"}
On the webapi side, I have the following class
public class PaginatedRequestCommand
{
public int Page { get; set; }
public int Take { get; set; }
public string PropName { get; set; }
public string SortOrder { get; set; }
}
And the WebApiConfig class is as follows
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
// skiped many other lines as they are not relevant
config.ParameterBindingRules.Insert(0, typeof(PaginatedRequestCommand),
x => x.BindWithAttribute(new FromUriAttribute()));
}
}
And so in Wep Api controller I have the following action method.
[HttpPost]
[HttpGet]
public PaginatedList<PatientCategory> PatCat(PaginatedRequestCommand cmd)
{
//......
}
So here I have PaginatedRequestCommand object correctly constructed and the properties Page, Take etc are correctly available. The Angularjs ajax call on the browser is
$http({
method: 'GET',
url: this.callParams.uri, // The URI
params: this.callParams.paginationOptions, // This is where the JSON that showed earlier goes.
headers: { 'Content-Type': 'application/Json' }
})
Every thing so far so good.
Now I want to pass in some more parameters. I want to include an array in the JSON as follows.
{"Page":1,"Take":10,"SortOrder":"Asc","PropName":"Id",
"wherePredicateParams":
[{"propName":"Name","val":"s"},
{"propName":"Description","val":"h"}
]
}
So you see that the "wherePredicateParams" is the additional object that I want to pass. Its an array. What modifications do I have to do on the WebApi side?
I have tried adding the one more property to the PaginatedRequestCommand class, public string[] wherePredicateParams { get; set; } so the full class is as follows.
public class PaginatedRequestCommand
{
public int Page { get; set; }
public int Take { get; set; }
public string PropName { get; set; }
public string SortOrder { get; set; }
public string[] wherePredicateParams { get; set; }
}
This is actually working, in the sense that the property wherePredicateParams of PaginatedRequestCommand object created by the api inside the action method of the above controller
is providing me with {"propName":"Name","val":"s"}
and {"propName":"Description","val":"h"}
. But the problem is I have to parse it my self and use. Is there a better way.
Then I have tried changing string[] to whereParam[] and defined a class
public class whereParam
{
public string propName { get; set; }
public string val { get; set; }
}
So you see the propName and val are null? What am I missing?