Pass javascript array to C# via json

痞子三分冷 提交于 2019-12-06 12:45:45

Solved similar issue by this way:

JS code (part of processing function):

    var ords = [];
    $(".order-count-input").filter(function() {
        return $(this).val() > 0;
    }).each(function() {
        ords.push({                
            GoodsId: $(this).attr("goodsId"),
            Amount: $(this).val()
        });
    });

    var data = {
        orders: ords,
        orderId: id
    };

    var params = {            
        url: actionUrl,
        type: "POST",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: JSON.stringify(data),
        success: function (data) {
            window.location.replace(data.redirect);
        }
    };

    $.ajax(params);

Controller action:

[HttpPost]
public JsonResult PostOrder(long orderId, PostOrderViewModel[] orders)

Model:

[Serializable]
public class PostOrderViewModel
{
    public long GoodsId { get; set; }

    public int Amount { get; set; }
}

I would first check that the values are sent from the client side. I would use the browser debugger. There you can set breakpoints and see if the variable you send (JSON.stringify(exportOptions)) has values.

You could use the F12 button (either from the Chrome, or Firefox) and use the javascript debugger. In chrome you can do it in "Script" tab. In Firefox you can do it in "Sources" tab.

Hope I helped!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!