0

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)
{

}

1 Answer 1

1

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)
{

}
Sign up to request clarification or add additional context in comments.

2 Comments

I am using this JSON {"CHits":[true,false],"MHits":[true,false]} in fiddler composer, and the controller does not get the data, shows NULL. Any idea?
OK, I forgot to pass the content negotiation stuff for the JSON. After adding the HTTP headers: Accept: application/json, text/javascript, /; q=0.01 Content-Type: application/json; charset=UTF-8 All works OK

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.