使用Ajax将复杂对象数组从视图传递到控制器

时间:2019-06-21 19:00:51

标签: javascript json ajax asp.net-mvc

在控制器中,我需要接收两个参数(详细信息和测试),一个是自定义对象的列表,另一个是字符串,当两个参数同时传递时,我只能传递一个参数(对象列表)我在控制器中收到空值。

生成的Json到控制器:

[{
    "detail": [{
        "tag": "PIC330_620%2F_.PV_Out%23Value",
        "color": "%2331c63e"
    }, {
        "tag": "A330_10%2F_.FbkFwdOut%23Value",
        "color": "%238edeed"
    }, {
        "tag": "TIC330_603%2F_.PV_Out%23Value",
        "color": "%23e8ea62"
    }, {
        "tag": "TI330_600%2F_.PV_Out%23Value",
        "color": "%23f7cbb4"
    }, {
        "tag": "TIC311_602%2F_.MV%23Value",
        "color": "%23ef935d"
    }, {
        "tag": "TIC311_602%2F_.PV_Out%23Value",
        "color": "%23f28a9b"
    }, {
        "tag": "TIC310_600%2F_.MV%23Value",
        "color": "%2385f968"
    }, {
        "tag": "TIC310_605%2F_.PV_Out%23Value",
        "color": "%2308d687"
    }],
    "test": "lolo"
}]
//Generate list of objects
function getViewDetail() {
        var details = [];
        var tag;
        var color;
        var detail;
        $('.tagContainer').each(function (i, obj) {
            tag = $(this).find('.tag_label').text();
            color = $(this).children('.colorpicker').val();
            detail = { tag: encodeURIComponent(tag), color: encodeURIComponent(color) };
            details.push(detail);

        });  
        return details;

    }
// Call Ajax
function sendParameters(){
    var details = getViewDetail();
                        var list = [];
                        list.push({ detail: details, test: 'lolo' });
                        list = JSON.stringify(list);
                            console.log(list);
                        jQuery.ajax({
                            url: '@Url.Action("SaveView", "Batch")',
                            async: false,
                            data: list,
                            contentType: 'application/json',
                            dataType: 'json',
                            type: 'POST',
                            success: function (result) {
                                if (!result.success) {
                                    showErrorMessage(result.title, result.message);
                                }
                                else {
                                    showSuccessMessage(result.title, result.message);
                                }
                            }
                        });
}

//In the controller (abbreviated)
public JsonResult SaveView(IEnumerable<Detail> detail, string test)
        {}

//class
public class Detail
    {
        string _tag;
        string _color;

        public string tag { get => _tag; set => _tag = value; }
        public string color { get => _color; set => _color = value; }
    }

1 个答案:

答案 0 :(得分:1)

尝试一下:

   ID ProductionDate    Status
0   1       1/4/2000  ReOpened
1   2       1/2/2000  ReOpened
2   4      1/25/2000  ReOpened

并通过ajax发送:

df = pd.np.where(df.Status.str.contains("ReOpened"), df.groupby(['ID']).first(),0)

您的操作签名需要两个参数,data = { detail: details, test: 'lolo' }; data = JSON.stringify(data); data: data, 。您传递的是一个对象列表,上面带有两个属性detailtest。有区别吗?简而言之,您的发布对象应为:

detail