I am sending a JSON message to a MVC method. When I debug it calls the method but it seems like the data is not there. In other words answerDetail is null.
Can someone give me some advice on what I may be doing wrong. Here's what I have:
The following MVC Controller method:
[HttpPost]
[ActionName("CheckAnswers")]
public void CheckAnswers(AnswerDetail2 answerDetail)
{
var a = answerDetail;
}
This message is sent to the method:
POST http://127.0.0.1:81/api/Question/CheckAnswers HTTP/1.1
Host: 127.0.0.1:81
Connection: keep-alive
Content-Length: 722
Accept: application/json, text/plain, */*
Origin: http://127.0.0.1:81
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36
Content-Type: application/json;charset=UTF-8
Referer: http://127.0.0.1:81/questions/5
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Cookie: __RequestVerificationToken=4puG_e0..B181
[{"answerId":5,"text":"<p>x</p>","correct":null,"response":false},
{"answerId":6,"text":"<p>xx</p>","correct":null,"response":false},
{"answerId":7,"text":"<p>xxx</p>","correct":null,"response":false}]
Here's my AnswerDetail2 class:
public class AnswerDetail2
{
public int answerId { get; set; }
public string text { get; set; }
public Nullable<bool> correct { get; set; }
public Nullable<bool> response { get; set; }
}
Update: Please note I changed the header. It's actually going to a Web API method so I think it's okay to post an object. The problem is it's not getting accepted.
POSTinstead ofPUT? Also, your POST data is an array ofAnswerDetail2, not a single instance.