如何使用Ajax将表单值传递给控制器​​(我在控制器部分需要哪种类型来接受发送的数据) [英] How to pass form value to controller using Ajax (Which type do i need in the controller part to accept sent data)

查看:44
本文介绍了如何使用Ajax将表单值传递给控制器​​(我在控制器部分需要哪种类型来接受发送的数据)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用serializeArray()方法将表单的数据转换为数组,然后将其作为参数传递给控制器​​的POST方法,但是我尝试了 string,string [],object,object[] 和最后但并非最不重要的 Question和Question [] (问题是我描述问题形状的ViewModal),但只有 Question 有效.但这不适用于视图中的多个问题.

I want to convert my form's data into an array using serializeArray() method and then I want to pass this to my controller's POST method as a parameter but I've tried string, string[], object, object[] and last but not least Question and Question[] (Question is my ViewModal that describes the shape of the Question) but only the Question works. But it doesn't work for more than one question in the view.

我做了 JSON.stringify($("ourForm").serializeArray()),它的工作是正确的,但是我仍然无法将字符串化的数据传递到控制器中.(尝试了我在此平台上看到的String和String [],但它们不起作用.)

I did the JSON.stringify($("ourForm").serializeArray()), it does it's job but still I can't pass the stringified data into controller. (tried the String and String[] which I saw on this platform but they don't work.)

我该怎么做呢,我真的需要一些帮助...

How can I do this job guys, I really need some help...

控制器:

[HttpPost]
    public ActionResult CreateExam(String[] formdata) // 
    {
        var abc = formdata;
        return RedirectToAction("index");

    }

JavaScript代码:

Javascript Code:

$("#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:

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代码来实现它)

And my form: (I used partialview because I need the users to add as many questions they want and I got it working with some JS code)

<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:

The 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>

推荐答案

JSON.stringify(formdata)完成此工作.但是

JSON.stringify(formdata) does the job. BUT

AJAX块上的数据必须像这样;

the data must be like this on the AJAX block;

data = {formdata = JSON.Stringify(formdata)}

这篇关于如何使用Ajax将表单值传递给控制器​​(我在控制器部分需要哪种类型来接受发送的数据)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆