AJAX发布int数组和序列化表单数据

时间:2013-11-15 10:28:05

标签: jquery ajax asp.net-mvc

我正在尝试发布表单数据和整数数组。我无法找到我在这段代码中出错的地方。请帮我找出错误

    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; }
}

1 个答案:

答案 0 :(得分:1)

尝试

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
    });
}