我是初学者,开发MVC Web应用程序。我尝试将嵌套的Json传递给MVC中的控制器,但是我遇到了传递json文件的问题。
我的模特就是这样;
public class SurveyViewModels
{
public string Question { get; set; }
public bool IsMultipleChoice { get; set; }
public bool IsSelectOneMoreThan { get; set; }
public List<string> Options { get; set; }
}
public class SurveyItems
{
public string SurveyTitle { get; set; }
public List<SurveyViewModels> SurveyViewModel { get; set; }
}
我的控制器就是这样;
[HttpPost]
public JsonResult PostQuestionAndOptions(SurveyItems SurveyItems)
{
}
在数组中添加SurveyViewModels类:
var SurveyViewModels= new Array();
$("#getButtonValue").click(function () {
......
var Options = new Array();
for (i = 1 ; i < counter; i++) {
Options[i - 1] = $('#textbox' + i).val();
}
var Data= {
"Question": Question,
"IsMultipleChoice": MultipleChoice,
"IsSelectOneMoreThan": SelectOneMoreThan,
"Options": Options
};
SurveyViewModels.push(Data);
发布按钮功能:
$("#publishButton").click(function () {
var Title = $('#Title').val();
var SendingData = {
"SurveyTitle": Title,
"SurveyViewModel": SurveyViewModels
};
alert(Title);
alert(SurveyViewModels);
$.ajax({
type: "POST",
url: "/Surveys/PostQuestionAndOptions",
data:SendingData,
success: function (data) {
alert();
},
dataType: "json",
traditional: true
});
});
但List SurveyViewModel为null。为什么SurveyViewModel的列表不会传递给控制器。
有人可以帮助我吗?
答案 0 :(得分:0)
我认为这就是你要找的东西
var SurveyViewModels= new Array();
$("#getButtonValue").click(function () {
......
var Options = new Array();
for (i = 1 ; i < counter; i++) {
Options[i - 1] = $('#textbox' + i).val();
}
var Data= {
"Question": Question,
"IsMultipleChoice": MultipleChoice,
"IsSelectOneMoreThan": SelectOneMoreThan,
"Options": Options
};
SurveyViewModels.push(Data);
$("#publishButton").click(function () {
var Titleobj = {"SurveyTitle": Title };
var SurveyViewModelsobj = {"SurveyViewModel":SurveyViewModels}
var SendingData = $.extend({}, Titleobj , SurveyViewModelsobj );
console.log(Title);
console.log(SurveyViewModels);
$.ajax({
type: "POST",
url: "/Surveys/PostQuestionAndOptions",
data:SendingData,
success: function (data) {
alert();
},
dataType: "json",
traditional: true
});
});
检查所有打开和关闭代码的大括号, 如果有帮助,请将答案标记为已接受