【发布时间】:2015-10-19 17:39:35
【问题描述】:
在我的 QUIZ 应用程序中,我想向 MVC 控制器发送一组对象(其中一个问题有四个答案和一个正确答案作为对象的属性),但它发送空值。解决这个问题的关键是对JSON对象进行字符串化,定义模型并获取参数作为定义的模型。是否有任何替代解决方案?
我的视图 UI 看起来像 this
//查看脚本
<script>
$(document).ready(function () {
$("#btnsubmit").click(function () {
createquestions();
});
function createquestions()
{
var things = [];
var nofques = $("#ddlnofquestions").val();//Coming from Dropdown Value
for (var i = 1; i <= nofques; i++) {
var obj = {
id: i,
question: CKEDITOR.instances[i.toString()].getData(),
answer1:$("#" + i + 1).val(),
answer2: $("#" + i + 2).val(),
answer3: $("#" + i + 3).val(),
answer4: $("#" + i + 4).val(),
correctanswer: $("#" + i + 5).val(),
};
things.push(obj);
}
var thingss = JSON.stringify({ "things": things });
$.ajax({
type: 'POST',
url:'Question/CreateQuestion',
async:true,
dataType: 'json',
contentType: "application/json; charset=utf-8",
data: { things: JSON.stringify(things) },
traditonal: true,
success: function (data) {
alert("Sucessfully Created");
},
});
}
});
</script>
C#:模型类
public class CreateQuestion
{
public int id { get; set; }
public string question { get; set; }
public string answer1 { get; set; }
public string answer2 { get; set; }
public string answer3 { get; set; }
public string answer4 { get; set; }
public string correctanswer { get; set; }
}
C#:控制器
public ActionResult CreateQuestion(List<CreateQuestion> things)
{
//where we try to get an array of objects
//Working Code......
return View();
}
【问题讨论】:
-
我认为这是一个很好的解决方案。也许你的问题更适合codereview.stackexchange.com
标签: javascript c# jquery ajax asp.net-mvc