user1615362
user1615362

Reputation: 3807

Passing JSON to WEB API Controller

I have this model for an MVC WEB API controller. What will be the corresponding JSON to match this model structure?

namespace CarEvaluator.Models
{
    [DataContract]
    public class Record
    {
        [DataMember]
        public List<Boolean> CHits { get; set; }

        [DataMember]
        public List<Boolean> MHits { get; set; }
    }
}




public void Post(Record record)
{

}

Upvotes: 0

Views: 368

Answers (1)

Lars Anundsk&#229;s
Lars Anundsk&#229;s

Reputation: 1355

Structure:

{"CHits":[true,false],"MHits":[true,false]}

Example:

var postObject = new Object();

// Initialize CHits and MHits as arrays
postObject.CHits = [];
postObject.MHits = [];

// push some items into the arrays
postObject.CHits.push(true);
postObject.CHits.push(false);
postObject.MHits.push(true);
postObject.MHits.push(false);

// serialize data to post
// this is what you set as data property in for instance jquery ajax
$.ajax({
    //other params, content type etc
    type: 'POST',
    data: JSON.stringify(postObject),
    ...
    });

if your parameter is null, you should try to add the [FromBody] attribute and decorate the method with httppost

[HttpPost]
public void Post([FromBody]Record record)
{

}

Upvotes: 1

Related Questions