user6934713 user6934713 - 22 days ago 5
AngularJS Question

http post - pass a javascript object to mvc controller

I am trying to pass a javascript object containing a string and a jstring to a mvc controller. This is my code and I am getting null value in the controller.
Thank you in advance for the help.

function setMultiQuestion(question, responses) {
qaObject = new questionAnswerObj('q', [{'correct':true,'answer':'A1'}, {'correct':true,'answer':'A2'}];);

$http.post(baseUrl + "Admin/insertMultiAnswers", { qaObject: qaObject })
.success(function (data, status, headers, config) {
})
.error(function (data, status, header, config) {
});
}

public ActionResult insertMultiAnswers(string[] qaObject)
{
Console.Write(qaObject);
}

Answer

You must send a string to server. Please use this:

$http.post(baseUrl + "Admin/insertMultiAnswers", { qaObject: JSON.stringify(qaObject) })

JSON.stringify turns a Javascript object into JSON text and stores that JSON text in a string.

View reference here