【问题标题】:Passing an array of objects to the controller using JSON使用 JSON 将对象数组传递给控制器
【发布时间】:2016-01-04 08:12:04
【问题描述】:

我的数组是这样的:

Array[2]
 0: Object
        StockNo: "1"
        InvoiceNo: "1234"
        MaterialName: "MaterialName1"
        PONo: "1234"
        PRNo: "2124"
        Project: "ProjectName"
        Qty: "1"
        Remarks: "Test"
        Supplier: "SupplierName"
        TotalAmount: "23"
        Type: "2"
        Unit: "23"

  1: Object 
        StockNo: "2"      
        InvoiceNo: "1234"
        MaterialName: "MaterialName2"
        PONo: "1234"
        PRNo: "2124"
        Project: "ProjectName2"
        Qty: "1"
        Remarks: "Test"
        Supplier: "SupplierName"
        TotalAmount: "23"
        Type: "2"
        Unit: "23"

这 2 个数组位于名为 "inventoryList" 的变量中 我希望它通过我的控制器。我用过

JSON.stringify(inventoryList)

但它不起作用。

这是我的脚本:

function addSomething() {
    var dateReceived = $('#DateReceived').val();
    $.ajax({
        url: "/MyController/Create",
        type: 'post',
        dataType: 'json',
        async: false,
        data: {
            'dateReceived': dateReceived,
            'rrList': JSON.stringify(inventoryList)
        },
        success: function (data) {
            if (data.errorMessage != '') {
                alert(data.errorMessage);
            }
            else {
                window.location.href = '/MyController/Index';
            }
        }
    })
}

【问题讨论】:

  • $_POST var 为空还是什么?
  • @Daimos,你确定你说的是带有标签的 asp.net-mvc-5 吗?
  • @Diamos 这不是 PHP ..

标签: jquery json asp.net-mvc-5


【解决方案1】:

我认为这应该可行。

在我看来,发送数据的内容类型应该是application/json,并且 async 不应该设置为 false,因为它会破坏 ajax 的行为。

所以我的建议是:

function addSomething() {
    var dateReceived = $('#DateReceived').val();
    $.ajax({
        url: "/MyController/Create",
        type: 'post',
        dataType: 'json',
        contentType: 'application/json', // <----add the content type
        //async: true, // you can remove it because default is true.
        data: {
            'dateReceived': dateReceived,
            'rrList': JSON.stringify(inventoryList)
        },
        success: function (data) {
            if (data.errorMessage != '') {
                alert(data.errorMessage);
            }
            else {
                window.location.href = '/MyController/Index';
            }
        }
    })
}

【讨论】:

  • 是的,你是对的。它正在工作。我现在的问题是通过日期时间感谢您的帮助。
【解决方案2】:

将 JSON 对象更改为

inventoryList=JSON.stringify({ 'inventoryList': inventoryList});

然后在你的控制器中

public void Create(DateTime dateReceived,List<object> inventoryList)
{

}

【讨论】:

    【解决方案3】:
    <html>
        <head>
            <title>Ajax Json</title>
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
        </head>
        <body>
            <h1>Well Come</h1>
            <hr>
            <div id="data">Response Add Hear...!!!</div>
        </body>
        <script type="text/javascript">
            $(document).ready(function(){
                $obj1={"StockNo":"1","InvoiceNo":"1123","MaterialName":"MaterialName1"};
                $obj2={"StockNo":"1","InvoiceNo":"1124","MaterialName":"MaterialName2"};
                $obj_array=[];
                $obj_array.push($obj1);
                $obj_array.push($obj2);
                $obj_array=JSON.stringify($obj_array);
                $.ajax({
                    url:'test.php',
                    method:'post',
                    async:false,
                    data:{"test":$obj_array},
                    success:function(data){
                        $("#data").html(data);
                    }
                });
            });
        </script>
    </html>
    

    服务器文件

    <?php
    echo "<pre>";
    print_r(json_decode($_REQUEST['test']));
    echo "</pre>";
    ?>
    

    【讨论】:

      猜你喜欢
      • 2018-09-25
      • 1970-01-01
      • 2021-03-01
      • 2017-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-24
      • 2017-03-17
      相关资源
      最近更新 更多