Javascript Ajax无法将表单id发送到MVC控制器
我对ajax有一个小问题。所以首先我要让你看看我的行动 表格: 以下是我的控制器操作:Javascript Ajax无法将表单id发送到MVC控制器,javascript,jquery,ajax,asp.net-mvc,parameter-passing,Javascript,Jquery,Ajax,Asp.net Mvc,Parameter Passing,我对ajax有一个小问题。所以首先我要让你看看我的行动 表格: 以下是我的控制器操作: public JsonResult AddQuestion(SurveyQuestion model, int id) { SurveyQuestion question = new SurveyQuestion(); question.Body = model.Body; question.CreatedOn = DateTime.Now; question.Modif
public JsonResult AddQuestion(SurveyQuestion model, int id)
{
SurveyQuestion question = new SurveyQuestion();
question.Body = model.Body;
question.CreatedOn = DateTime.Now;
question.ModifiedOn = DateTime.Now;
question.Priority = 0;
question.SurveyId = id;
question.Text = model.Text;
question.Type = QuestionType.Text;
_db.SurveyQuestions.Add(question);
_db.SaveChanges();
return Json(question, JsonRequestBehavior.AllowGet);
}
我没有填写代码的每一部分,其中一些是硬编码的,因为我将在稍后执行操作。但问题在于发送两件东西。
如果我不从控制器发送Id和delete Id,它会很好地发送序列化模型,但如果我同时发送它们,它会获取Id,但不会将模型发送给他(文本和正文为null)。我该如何解决这个问题 您可以按如下方式进行:
$("#btnSubmit").click(function() {
$.ajax({
type: "POST",
url: "/Survey/AddQuestion",
// Add id like query string parameter like below
data: $('#QuestionForm').serialize() + '&id=' + @Model.Survey.Id,
success: function() {
$("#AddQuestion").modal("hide");
//alert("Survey Added");
location.reload();
}
})
})
您可以在url中输入id
$("#btnSubmit").click(function () {
var data = $('#QuestionForm').serialize();
$.ajax({
type: "POST",
url: "/Survey/AddQuestion?id=@Model.Survey.Id",
data: {data},
success: function () {
$("#AddQuestion").modal("hide");
//alert("Survey Added");
location.reload();
}
})
})
传递id作为查询字符串参数
$("#btnSubmit").click(function () {
var data = $('#QuestionForm').serialize();
$.ajax({
type: "POST",
url: "/Survey/AddQuestion?id=" + '@Model.Survey.Id',
data: {data, @Model.Survey.Id},
success: function () {
$("#AddQuestion").modal("hide");
//alert("Survey Added");
location.reload();
}
})
})
public JsonResult AddQuestion(SurveyQuestion model,[FromUri]int id)
{
SurveyQuestion question = new SurveyQuestion();
question.Body = model.Body;
question.CreatedOn = DateTime.Now;
question.ModifiedOn = DateTime.Now;
question.Priority = 0;
question.SurveyId = id;
question.Text = model.Text;
question.Type = QuestionType.Text;
_db.SurveyQuestions.Add(question);
_db.SaveChanges();
return Json(question, JsonRequestBehavior.AllowGet);
}
在控制器中,在参数之前使用FromUri属性
$("#btnSubmit").click(function () {
var data = $('#QuestionForm').serialize();
$.ajax({
type: "POST",
url: "/Survey/AddQuestion?id=" + '@Model.Survey.Id',
data: {data, @Model.Survey.Id},
success: function () {
$("#AddQuestion").modal("hide");
//alert("Survey Added");
location.reload();
}
})
})
public JsonResult AddQuestion(SurveyQuestion model,[FromUri]int id)
{
SurveyQuestion question = new SurveyQuestion();
question.Body = model.Body;
question.CreatedOn = DateTime.Now;
question.ModifiedOn = DateTime.Now;
question.Priority = 0;
question.SurveyId = id;
question.Text = model.Text;
question.Type = QuestionType.Text;
_db.SurveyQuestions.Add(question);
_db.SaveChanges();
return Json(question, JsonRequestBehavior.AllowGet);
}
试试这个,在post中传递多个参数,或者可以使用querystring传递,如其他答案所示
$("#btnSubmit").click(function () {
var data = $('#QuestionForm').serialize();
$.ajax({
type: "POST",
url: "/Survey/AddQuestion",
data: {model : data, id: @Model.Survey.Id},
success: function () {
$("#AddQuestion").modal("hide");
//alert("Survey Added");
location.reload();
}
})
})
控制器
public ActionResult ActionName(SurveyQuestion model, int id)
{
Your Code .......
}
您不能将id作为查询字符串传递吗?您可以将其粘贴为答案吗?谢谢你这么做了,你可以补充说Fromuri不起作用,我已经试过了,但我已经得到了我问题的答案,谢谢!对不起,我忘记了“?”后面的id参数,