[英]How to pass form value to controller using Ajax (Which type do i need in the controller part to accept sent data)
我想使用serializeArray()方法將我的表單數據轉換為數組,然后我想將它作為參數傳遞給我的控制器的POST方法,但我已經嘗試了字符串,字符串[],對象,對象[]和最后但不是最少問題和問題[] (問題是我的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>
您需要在ajax調用上相應地設置內容類型。
JSON.stringify(formdata)完成這項工作。 但
AJAX塊上的數據必須是這樣的;
data = {formdata = JSON.Stringify(formdata)}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.