http投稿でjson配列と値を渡そうとしています。値は渡されましたが、配列は渡されませんでした。 javascriptコントローラのrespondListの値は、 "[{" correct ":" false "、" answer ":" a1 "}、{" correct ":true、" answer ":" a2 "}]"httpポストを持つ角型配列
ジャバスクリプト制御
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コントローラ
[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);
}
}
なぜあなたはこれをやっていますか? 'var respondList = {'object':jsonData} ;;'? –
jsで参照しているメソッド 'insertMultiAnswers'はどこにありますか? –
2つのうちの1つを行うことができます.Jsonオブジェクトとして保持し、FromBody属性をエンドポイントに追加するか、json.stringify()を使用してjson文字列に変換し、エンドポイントにできる文字列として取り込みますそれをオブジェクトにシリアル化し直します。私は最初の方法を好む。 – jbrown