如何使用AJAX将包含多个PartialViews的表单传递给控制器

时间:2019-06-05 13:17:24

标签: javascript jquery .net ajax model-view-controller

我需要向控制器发送数据,但是由于我使用的是“添加新问题”按钮,该按钮以 _QuestionLayout 的名称添加了新问题模块。我想做的是,我希望我的用户创建他/她想要的问题-最多25- [我的代码到目前为止可以完成],然后,当他/她单击“创建考试”按钮时,我希望将使用partialviews创建的所有表单数据发送到控制器。

我不知道该怎么做,我们将不胜感激。

我的主视图:

@model OnlineSinav.Areas.Teacher.ViewModels.Questions
@{
    ViewBag.Title = "CreateExam";
}

<div class="col-lg-12" id="questions">
    <div id="abc">
        <form class="form-horizontal style-form" id="ourForm">
            <div class="form-panel">
                <button type="button" class="btn btn-success newQuest"><i class="fa-plus">  ADD NEW QUESTION</i></button>
                @Html.Partial("_QuestionLayout", Model)
            </div>
            <button type="button" class="btn btn-primary" id="submitForm">CREATE EXAM</button>
        </form>
    </div>
</div>

<script src="../../../Content/lib/jquery/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        var max_fields = 25;
        var wrapper = $(".form-panel");

        var partialHTML = `@Html.Partial("_QuestionLayout", Model)`;

        var x = 1;

        $(wrapper).on("click", ".delete", function (e) {
            e.preventDefault();
            $(this).parent('div').parent('div').parent('div').remove();
            x--;
        });

        $(wrapper).on("click", ".newQuest", function (e) {
            e.preventDefault();
            if (x < max_fields) {
                x++;
                $(wrapper).append(partialHTML); //add input box
            } else {
                alert('You Reached the limits')
            }
        });


        $("#submitForm").click(function () {
           var formdata = $("#ourForm").serializeArray();
            $.ajax({
                type: "POST",
                url: "/Teacher/Exam/CreateExam",
                data: formdata,
                success: function (data) {
                    alert("DONE");
                    // Do something here when it is finished
                }
            });
        });
    });
</script>

我的PartialView:

@model OnlineSinav.Areas.Teacher.ViewModels.Questions


<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">  DELETE ADDED QUESTION</i></button>
            <button type="button" class="btn btn-success newQuest pull-right"><i class="fa-plus">  ADD NEW QUESTION</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 = "Question text will be placed here.", 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>

我的控制器(目前不执行任何操作,但是我希望它获取数据作为参数,我不知道如何,然后在这里进行编码以将数据发送到数据库)

[HttpPost]
public ActionResult CreateExam(string formdata) // 
{

      return RedirectToAction("index");
}

如果需要,我的ViewModel

public class Questions
    {        
        public IList<Questions> questions { get; set; }
        [Display(Name ="SORU ALANI")]
        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 ="DOĞRU CEVAP")]
        public string correct_answer { get; set; }
    }

问题是,如何将所有数据作为数组发送到控制器。最好是IList类型,但不要紧,简单的字符串数组可以为我完成工作,在此之后我可以使用它,在此先感谢大家。

0 个答案:

没有答案