我正在尝试将表格数据与整数array一起发布。我无法找到我在此代码中做错的地方。请帮助我找到错误

    var DetailID = new Array();

    $.each(selected, function (i, n)
    {
        var detailID = $(this).attr('data-record-key');
        DetailID.push(detailID);

    });
    var data = $('#form').serialize();
    var finaldata = data + '&' + DetailID;

    if ($('#form').valid())
    {
        $.ajax({
            url: $('#form').attr('action'),
            type: $('#form').attr('method'),
            traditional: true,
            dataType: "json",
            data:finaldata,
            contentType: 'application/json; charset=utf-8',
            success: onSuccess,
            error: onFailure
        });
    }


我的控制器:

public ActionResult Merge(Merge merge)
    {

    }


我的模特:

public class Merge
{
    public string ItemCode { get; set; }

    public string ItemDescription { get; set; }

    public int[] DetailID { get; set; }
}

最佳答案

尝试

var finaldata = data + '&' + $.param({DetailID: DetailID});

if ($('#form').valid())
{
    $.ajax({
        url: $('#form').attr('action'),
        type: $('#form').attr('method'),
        //traditional: true, -- finaldata is a string so this doesn't matter
        dataType: "json", // are you returning a json response?
        data: finaldata,
        //contentType: 'application/json; charset=utf-8', -- you aren't sending json
        success: onSuccess,
        error: onFailure
    });
}

关于jquery - AJAX Post int数组和序列化表格数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19998892/

10-09 23:25