通过ajax将对象列表传递给MVC控制器,始终发送null

时间:2018-10-16 13:16:56

标签: json ajax

我可能缺少一些非常简单的东西。我已经为此工作了一天半,无法使它正常工作。我正在遍历一个表并创建对象列表以发送回我的控制器。由于某种原因,我总是在控制器中收到一个空值。这是Java脚本。

 var items = [];
        $('#grid tr').each(function () {
            var item = {};
            item.numReceived = $(this).find("input[id*='NumReceived']").val();
            /*skip the header row*/
            if (item.numReceived !== null) {
                item.transactionID = $(this).find("input[id*='item_TransactionID']").val();
                items.push(item);
            }
        });

        $.ajax({
            url: './ReceivePOLines',
            type: "Post",
            cache: false,
            data: JSON.stringify(items),
            dataType: "json",
            contentType: 'application/json; charset=utf-8',
            success: function () {
                window.location.replace("../Home/Index");
            },
            error: function (request) {
                alert("error");
            }
        });

这是控制器中的方法签名

 [HttpPost]
  public void ReceivePOLines(List<RecievedTransactions> inTransactions)

这是类ReceivedTransactions

  public class RecievedTransactions{
      public int numReceived { get; set; }
      public int transactionID { get; set; }
   }

这是Fiddler的结果,显示通过了什么

[{},{“ numReceived”:“ 10000”,“ transactionID”:“ 10661768”},{“ numReceived”:“ 10000”,“ transactionID”:“ 10661769”},{“ numReceived”:“ 2000 “,” transactionID“:” 10661770“},{” numReceived“:” 2500“,” transactionID“:” 10661771“},{” numReceived“:” 2500“,” transactionID“:” 10661772“},{” numReceived “:” 2000“,” transactionID“:” 10661773“},{” numReceived“:” 10000“,” transactionID“:” 10661774“}]

任何人和所有帮助表示赞赏。 干杯 鲍勃

1 个答案:

答案 0 :(得分:1)

这是一个新答案。最初,我像你一样变得空虚。但是,现在它可以按您想要的方式工作(复杂对象的数组)。请为您工作。如果您不能使它正常运行,尽管可以,我可以创建一个ASP.NET Fiddle。

public class RecievedTransactions
{
    public int numReceived { get; set; }
    public int transactionID { get; set; }
}

public class HomeController : Controller
{
    [HttpPost]
    public void ReceivePOLines(List<RecievedTransactions> inTransactions)  // MyArray MyArray
    {
        return;
    }

    //you use your own action name
    public ActionResult Tut133()
    {
        return View();
    }

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Tut132</title>
    <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
    <script type="text/javascript">
        $(function () {
            var items = [];
            $('#grid tr').each(function () {
                var item = {};
                item.numReceived = $(this).find("input[id*='NumReceived']").val();
                /*skip the header row*/
                if (item.numReceived !== null) {
                    item.transactionID = $(this).find("input[id*='item_TransactionID']").val();
                    items.push(item);
                }
            });

            $.ajax({
                //!!changing your url
                //url: './ReceivePOLines',
                url: "/Home/ReceivePOLines",
                type: "Post",
                cache: false,
                //data: JSON.stringify({ MyArray: items }),
                data: JSON.stringify(items),
                //expecting back from server-need to remove since we are not getting back data from server
                //dataType: "json",
                contentType: 'application/json; charset=utf-8',
                success: function () {
                    //alerting success instead of opening window
                    alert("success");
                    //window.location.replace("../Home/Index");
                },
                error: function (request) {
                    alert("error");
                }
            });
        })
    </script>
</head>
<body>
    <table id="grid">
        <tr>
            <td><input type="text" id="NumReceived1" value="10000" /></td>
            <td><input type="text" id="item_TransactionID1" value="10661768" /></td>
        </tr>
        <tr>
            <td><input type="text" id="NumReceived2" value="10000" /></td>
            <td><input type="text" id="item_TransactionID2" value="10661769" /></td>
        </tr>
    </table>
    <input type="button" id="theButton" value="Go" />
</body>
</html>