I am trying to pass a json array and a value with http post. The value was passed but the array not. The value of responsesList in the javascript controller is : "[{"correct":"false","answer":"a1"},{"correct":true,"answer":"a2"}]"
javascript controller
function setMultiQuestion(question, responses)
{
var jsonData = angular.toJson(responses);
var responsesList = {'object':jsonData};;
$http.post(baseUrl + "Admin/insertMultiAnswers", { question: question, responsesList: responsesList })
.success(function (data, status, headers, config) {
})
.error(function (data, status, header, config) {
});
}
MVC Controller
[HttpPost]
public ActionResult insertMultiAnswers(MultiChoiceQuestionModel model)
{
try
{
model.setMultiAnswer();
Response.StatusCode = 200;
return Content("Sucess");
}
catch (Exception ex)
{
Response.StatusCode = 500;
return Content("Fail");
}
}
MODEL
public class answerObj
{
public bool correct { get; set; }
public string answer { get; set; }
}
public class MultiChoiceQuestionModel
{
public string question { get; set; }
public List<answerObj> responsesList = new List<answerObj>();
public void setMultiAnswer()
{
using (ATLASEntities atlasEntity = new ATLASEntities())
{
Console.Write(responsesList.Count);
}
}
angular.toJson(responses)
serializes to string not to an object. You should just do
responsesList: responses
In order to be mapped to a list the object from the json needs to be an array.
So, if you change below
var responsesList = {'object':jsonData};;
to something like:
var responsesList = [{'correct':true,'answer':'Meow'}, {'correct':true,'answer':'Woof'}];
It will work.
I assume that you get plain array ( []
) from responses
and not array that is inside of string value ( "[]"
). Try this:
function setMultiQuestion(question, responses){
var myObj = {};
myObj = JSON.stringify({question: question, responsesList: responses});
$http.post(baseUrl + "Admin/insertMultiAnswers", myObj)
.success(function (data, status, headers, config) {
})
.error(function (data, status, header, config) {
});
}
You might have to parse that string in your backend.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.