我希望使用serializeArray()方法将表单数据转换为数组,然后将其作为参数传递给控制器的POST方法,但我尝试了string、string[]、object、object[]和最后但并非最不重要的array和Question[] (array是描述问题形状的ViewModal ),但只有问题有效。但它不适用于视图中的多个问题。
我做了JSON.stringify($("ourForm").serializeArray()),,它完成了它的工作,但是我仍然不能把字符串数据传递给控制器。(尝试了我在此平台上看到的String和String[],但它们不起作用。)
我该怎么做呢,伙计们,我真的需要一些帮助...
控制器:
[HttpPost]
public ActionResult CreateExam(String[] formdata) //
{
var abc = formdata;
return RedirectToAction("index");
}
Javascript代码:
$("#submitForm").click(function () {
var formdata = $("#ourForm").serializeArray();
$.ajax({
type: "POST",
url: "/Teacher/Exam/CreateExam",
traditional: true,
data: formdata,
success: function (data) {
alert("BAŞARILI?");
// Do something here when it is finished
}
});
});
ViewModal:
public class Questions
{
public IList<Questions> questions { get; set; }
[Display(Name ="QUESTION GOES HERE")]
public string question_string { get; set; }
public string A { get; set; }
public string B { get; set; }
public string C { get; set; }
public string D { get; set; }
public string E { get; set; }
[Display(Name ="CORRECT ANSWER")]
public string correct_answer { get; set; }
}
和我的表单:(我使用了partialview,因为我需要用户添加他们想要的问题,并且我使用了一些JS代码)
<form class="form-horizontal style-form" id="ourForm">
<div class="form-panel">
<button type="button" class="btn btn-success newQuest"><i class="fa-plus"> Yeni Soru Ekle</i></button>
@Html.Partial("_QuestionLayout", Model)
</div>
<button type="button" class="btn btn-primary" id="submitForm">SAVE THE EXAM</button>
</form>
ViewModal:
<div id="newForm">
<div class="form-group" style="margin-right: 0px;">
<div class="col-lg-12">
<hr />
<button type="button" class="btn btn-danger delete pull-right"><i class="fa-plus"> SORUYU SİL</i></button>
<button type="button" class="btn btn-success newQuest pull-right"><i class="fa-plus"> YENİ SORU EKLE</i></button>
@Html.LabelFor(m => m.question_string, new { @class = "control-label col-md-3" })
@Html.TextAreaFor(m => m.question_string, new { @class = "form-control", placeholder = "Soru buraya yazılacak.", style = "max-width:100%; min-width:100%" })
</div>
</div>
<div class="form-group">
<div class="col-md-8 col-xs-11">
@Html.LabelFor(m => m.A, new { @class = "col-sm-2 col-sm-2 control-label" })
@Html.TextBoxFor(m => m.A, new { @class = "col-sm-10" })
</div>
</div>
<div class="form-group">
<div class="col-md-8 col-xs-11">
@Html.LabelFor(m => m.B, new { @class = "col-sm-2 col-sm-2 control-label" })
@Html.TextBoxFor(m => m.B, new { @class = "col-sm-10" })
</div>
</div>
<div class="form-group">
<div class="col-md-8 col-xs-11">
@Html.LabelFor(m => m.C, new { @class = "col-sm-2 col-sm-2 control-label" })
@Html.TextBoxFor(m => m.C, new { @class = "col-sm-10" })
</div>
</div>
<div class="form-group">
<div class="col-md-8 col-xs-11">
@Html.LabelFor(m => m.D, new { @class = "col-sm-2 col-sm-2 control-label" })
@Html.TextBoxFor(m => m.D, new { @class = "col-sm-10" })
</div>
</div>
<div class="form-group">
<div class="col-md-8 col-xs-11">
@Html.LabelFor(m => m.E, new { @class = "col-sm-2 col-sm-2 control-label" })
@Html.TextBoxFor(m => m.E, new { @class = "col-sm-10" })
</div>
</div>
<div class="form-group">
<div class="col-md-8 col-xs-11">
@Html.LabelFor(m => m.correct_answer, new { @class = "col-sm-2 col-sm-2 control-label" })
@Html.TextBoxFor(m => m.correct_answer, new { @class = "col-sm-1" })
</div>
</div>
</div>
https://stackoverflow.com/questions/56467236
复制相似问题