使用AJAX将表单传递给控制器​​MVC

时间:2018-11-20 13:35:54

标签: jquery asp.net-mvc

想问一下如何使用AJAX将表单传递给控制器​​MVC。 我尝试了各种方法,例如JSON.stringify,对我没有任何帮助。我的输入名称是一个数组。例如,Received [0] .basketball,Received [1] .basketball,Received [2] .basketball。因此,列表应具有Count = 3,但列表仍然为空。

更新:包括表单ID

public ActionResult Validate(List<Stuff> item)
{
   //null
}

@using (Html.BeginForm("Test", "Test", FormMethod.Post, new { id = "form" }))
{
    <input name="Receive[0].basketball" id="basketball' + x + '" value=""/>
    <input name="Receive[0].ball" id="ball' + x + '" value=""/>
    <input name="Receive[1].basketball" id="basketball' + x + '" value=""/>
    <input name="Receive[1].ball" id="ball' + x + '" value=""/>
}

$.ajax({
        type: "POST",
        url: "../../Validate",
        dataType: 'json',
        data: {
            item:  $('#form').serializeArray(),
        },
        success: function (result) {

        }
    });

2 个答案:

答案 0 :(得分:0)

您可以在此处使用 // Apply the java-library plugin to add support for Java Library apply plugin: 'java-library' // In this section you declare where to find the dependencies of your project repositories { // Use jcenter for resolving your dependencies. // You can declare any Maven/Ivy/file repository here. jcenter() } dependencies { // This dependency is used internally, and not exposed to consumers on their own compile classpath. implementation 'com.google.protobuf:protobuf-java:3.5.1' implementation 'com.google.protobuf:protobuf-gradle-plugin:0.8.7' // Use JUnit test framework testImplementation 'junit:junit:4.12' }

serialize()

答案 1 :(得分:0)

 var form = $('#form');
 var formData = $(form).serialize();

 $.ajax({
            type: 'POST',
            url: $(form).attr('action'),
            data: formData,
            success: function (result) 
        {

    }
});