将jquery中的数组发布到控制器会在行中返回任何内容(null)

时间:2018-02-28 16:22:37

标签: jquery asp.net-mvc-5

动态表生成这样的标记

//Outstanding invoices check changed
$('body').on('change', '.custom-control-input', function () {
//Send to controller to work out total of selected rows and total
var vTable = new Array();
$('#OutstandingInvoicesTable tbody tr').each(function () {
    var row = $(this);
    var rows = {};
    rows.ID = row.find("td").eq(0).html();
    rows.Debit = row.find("td").eq(3).html();
    rows.Credit = row.find("td").eq(4).html();
    rows.Selected = row.find("input").eq(0).prop('checked');
    //alert(rows.Selected);
    vTable.push(rows);

});
jQuery.ajax({
    url: '@Url.Action("SelectInvoicesValuesChanged", "Account")',
    type: 'POST',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    traditional: true,
    data: JSON.stringify(vTable),
    success: function (response) {
        ModalSuccess(response);
    },

    error: function (XMLHttpRequest, textStatus, errorThrown) {
        ModalError(textStatus + " - " + errorThrown);
    }

})

})

发布数组的jquery

 <HttpPost()>
    Function SelectInvoicesValuesChanged(vList() As List(Of String)) As ActionResult
        Try

            Dim vRows As Integer = vList.Count



            Return Json(vRows)
        Catch ex As Exception
            EmailError(ex, 846, PageName)
            Return Json("Error")
        End Try
    End Function

似乎有效 - 警报将始终返回正确的值 - 但是当它到达控制器时

launch_shinystan

它返回正确的行数,但每一行都没有!

Debug image

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

  1. 添加课程(模型)

    Public Class AccountModel
    
    
    Public Class InvoiceTransactions
    Public Property TransactionID() As String
        Get
            Return v_TransactionID
        End Get
        Set(value As String)
            v_TransactionID = value
        End Set
    End Property
    Private v_TransactionID As String
    
    Public Property Debit() As String
        Get
            Return v_Debit
        End Get
        Set(value As String)
            v_Debit = value
        End Set
    End Property
    
    Private v_Debit As String
    
    Public Property Credit() As String
        Get
            Return v_Credit
        End Get
        Set(value As String)
            v_Credit = value
        End Set
    End Property
    Private v_Credit As String
    
    Public Property Selected() As Boolean
        Get
            Return v_Selected
        End Get
        Set(value As Boolean)
            v_Selected = value
        End Set
    End Property
    
    Private v_Selected As Boolean
    
    End Class
    
    
    Public Property Transaction() As List(Of InvoiceTransactions)
    Get
        Return v_InvoiceTransactions
    End Get
    Set(value As List(Of InvoiceTransactions))
        v_InvoiceTransactions = value
    End Set
    End Property
    
    Private v_InvoiceTransactions As List(Of InvoiceTransactions)
    
    
    End Class
    
  2. 修改控制器功能

    <HttpPost()>
    Function SelectInvoicesValuesChanged(vList As List(Of AccountModel.InvoiceTransactions)) As ActionResult
        Try
            Dim vTotal As Decimal = 0
            Dim vRows As Integer = vList.Count
            If vRows > 0 Then
                For Each Row In vList
                    If Row.Selected = True Then
                        Dim vID As Integer = Row.TransactionID
                        Dim vDebit As Decimal = Row.Debit
                        Dim vCredit As Decimal = Row.Credit
                        vTotal += vDebit - vCredit
                    End If
                Next
            End If
    
            Return Json(vTotal)
        Catch ex As Exception
            EmailError(ex, 846, PageName)
            Return Json("Error")
        End Try
    End Function