繁体   English   中英

如何使用Ajax将表单值传递给控制器​​(控制器部分需要哪种类型来接受发送的数据)

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM