所以,我有一个 JS .post 调用,它通过控制器操作向表中添加一行。然后它将该行的主键返回给视图。我需要将该整数插入数据中,以便我需要对另一个控制器进行第二次 .post 调用。
更新的 Javascript 首先是看看我的 Javascript:
var result = $.post('/Question/CreateSimpleQuestion/', (data), function (result) {
$.post('/Question/CreateSimpleQuestionChoice/', ({
"QuestionId":result,
"DisplayText": text,
"OrderNumber": order,
"is_correct": false}),
null, 'application/json');
}, 'application/json');
这些是被调用的控制器动作:
//
// POST: /Question/CreateSimpleQuestion
[HttpPost]
public JsonResult CreateSimpleQuestion(Question question)
{
question.is_counted = true;
question.DateCreated = DateTime.Now;
db.Questions.Add(question);
db.SaveChanges();
return Json(question.QuestionId, JsonRequestBehavior.AllowGet);
}
//
// POST: /Question/CreateSimpleQuestion
[HttpPost]
public JsonResult CreateSimpleQuestionChoice(QuestionChoices choice)
{
db.QuestionChoices.Add(choice);
db.SaveChanges();
return Json(choice, JsonRequestBehavior.AllowGet);
}