2015-10-07 49 views
1

對象到控制器的陣列我的數組是這樣的:傳遞使用JSON

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'; 
      } 
     } 
    }) 
} 
+0

$ _ POST var爲空或有什麼? – Daimos

+0

@Daimos,你確定你在談論asp.net-mvc-5,因爲它標有標籤嗎? – Alex

+0

@Diamos它不是PHP .. –

回答

0

變化JSON對象

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

然後在你的控制器

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

} 
0

,我看到,應該工作。

在我看來,發送數據的內容類型應該是application/json,異步不應設置爲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'; 
      } 
     } 
    }) 
} 
+0

是的,你是對的。它正在工作。我現在的問題是通過Datetime感謝您的幫助。 –

0
<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>"; 
?> 
+0

請參閱標籤先生。謝謝.. –