首页 > 解决方案 > 如何通过ajax将两个数组发送到控制器

问题描述

我尝试将两个数组发送到控制器,每个数组来自不同的类。但我得到的只是一条错误消息的警报。我究竟做错了什么?当我在 ajax 数据中只发送一个数组时,它在控制器的第一个数组中得到很好。我的代码js:

    $("#Button2").click(function () {
        var dict = new Array();
        $(":checkbox").each(function () {
            if ($(this).prop("checked")== true) {

                var key = this.name 
                if ($("input[name = 'r" + key + "']").length) {                          
                    dict.push({
                        Code: key,
                        Reccomendation: $("input[name = 'r" + key + "']").prop("value"),                         
                    });
                }                                          
                else{
                    dict.push({
                        Code: key,
                        Reccomendation: $(this).prop("value"),                          
                    });

                }
            }

        }) //end function each
        var dict2 = new Array();

        dict2.push({
            Mentioned:  $("#yesno").val(),
            FollowUp: $("#Follo").val(),
            UpdateCode:5
        })


    $.ajax({
        type: 'POST',
        url: "@Url.Action("SavevisitSummary")",
        traditional: true,
        dataType: 'JSON',
        contentType: 'application/json; charset=utf-8',
        data: { 'a': JSON.stringify(dict), 'b': JSON.stringify(dict2) },

        success: function () {
            alert("sucssec")

        },
        error:function(){
            alert("error")
        }
    })

 })

控制器看起来像:

public ActionResult SavevisitSummary(Reccomendations[] a, Summary[] b) { }

标签: javascriptasp.net-mvc-5asp.net-ajax

解决方案


推荐阅读