Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

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; }
}

enter image description here So you see the propName and val are null? What am I missing?

share|improve this question

1 Answer 1

The property wherePredicateParams is a string[] not a whereParam[] so this make that the serializer dont serialize this properties, but I suggest you to use Json.NET you can get it here http://www.newtonsoft.com/json, this lib allows that you use Attributes on your properties that map your json schema to a class/type.

You can do something like the example:

public class WhereParam 
{
    [JsonProperty("propName")]
    public string PropName { get; set; }
    [JsonProperty("val")]
    public string Val { get; set; }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.