jQuery填充了Gridview在回发时丢失数据

时间:2018-03-22 14:29:32

标签: jquery asp.net vb.net

我有一个gridview,我使用javascript(jQuery)填充客户端,我需要在回发时将数据提供给服务器。显然,任何经过数据处理的客户端都会在正常的回发中丢失,寻找在回发时保留客户端数据的最佳方法。

我的月度付款计划的jQuery代码(完美运行):

totalmonths = moment(paymentbydate).diff(today, 'month');
var totalamount = ((amountdue / totalmonths) + ((amountdue / totalmonths) * processingfee));

var weeklyamount = toFixed(amountdue / totalmonths, 2);
var paymentprocessingfee = toFixed((amountdue / totalmonths) * processingfee, 2);
var lastpayment = 0;
totalamount = toFixed(parseFloat(weeklyamount) + parseFloat(paymentprocessingfee), 2); //(amountdue/totalweeks) + ((amountdue / totalweeks) * processingfee);

// Add first payment to grid
$("#<%=GridView_PaymentSchedule.ClientID%>").append("<tr><td>" + todaydate + "</td><td>" + weeklyamount + "</td><td>" + paymentprocessingfee + "</td><td>" + totalamount + "</td></tr>");
// Add second payment, startdate, to grid
var nextdate = moment(datevalue, 'MM/DD/YYYY').add(1, 'months').format('MM/DD/YYYY');
$("#<%=GridView_PaymentSchedule.ClientID%>").append("<tr><td>" + datevalue + "</td><td>" + weeklyamount + "</td><td>" + paymentprocessingfee + "</td><td>" + totalamount + "</td></tr>");

var totalpaymentsmade = parseFloat(totalamount) * 2;
// Total up payment so you can calculate overage/underpayment if any
// add all subsequet payments until paid in full
for (i = 0; i < (totalmonths - 2) ; i++) {
    totalpaymentsmade = totalpaymentsmade + parseFloat(totalamount); // Total up the next payment
    var nextpayment  = toFixed(totalpaymentsmade - amountduewithfee, 2);
    if (nextpayment == 0) {
        $("#<%=GridView_PaymentSchedule.ClientID%>").append("<tr><td>" + nextdate + "</td><td>Final Payment: " + weeklyamount + "</td><td>" + paymentprocessingfee + "</td><td>" + totalamount + "</td></tr>");
        break;
    } else {
        $("#<%=GridView_PaymentSchedule.ClientID%>").append("<tr><td>" + nextdate + "</td><td>" + weeklyamount + "</td><td>" + paymentprocessingfee + "</td><td>" + totalamount + "</td></tr>");
    }
    nextdate = moment(nextdate, 'MM/DD/YYYY').add(1, 'months').format('MM/DD/YYYY');
    numpayments += 1;
    // check if next payment is overpayment or underpayment but under payment amount
    lastpayment = toFixed(amountduewithfee - totalpaymentsmade, 2);
    if (totalpaymentsmade > amountduewithfee) {
        $("#<%=GridView_PaymentSchedule.ClientID%>").append("<tr><td>" + nextdate + "</td><td>Final Payment</td><td>0</td><td>" + lastpayment + "</td></tr>");
        break;
    } else if ((amountduewithfee - totalpaymentsmade) < amountduewithfee && (amountduewithfee - totalpaymentsmade) < totalamount) {
        $("#<%=GridView_PaymentSchedule.ClientID%>").append("<tr><td>" + nextdate + "</td><td>Final Payment</td><td>0</td><td>" + lastpayment + "</td></tr>");
        break;
    } else if (totalpaymentsmade == amountduewithfee) {
        $("#<%=GridView_PaymentSchedule.ClientID%>").append("<tr><td>" + nextdate + "</td><td>Final Payment</td><td>0</td><td>" + lastpayment + "</td></tr>");
        break;
    }
}

更新 经过一些研究,似乎处理这种情况的最好方法是使用ajax通过webmethod将数据发布到服务器,但是现在当我用一个字符串数组调用方法时,它执行时没有错误但是在服务器端没有收到任何数据,我得到一个空列表。

我的ajax电话:

$.ajax({
    type: "POST",
    url: "PaymentPlan.aspx/SaveGridViewData",
    data: '{pPayments: ' + JSON.stringify({ payments: payments }) + '}',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    traditional: true,
    success: function (response) {
    alert("Data sucessfully transmited.");
        window.location.reload();
    },
    error: function (response) {
        alert("Data not transmited: error = " + response.responseText);
    }
});

付款是一系列数组,[日期,金额,费用,总额],所有字符串都可以简化。

背后的代码: 我保持简单,我在x += 1上设置断点来检查参数。

<WebMethod()> _
Public Shared Sub SaveGridViewData(ByVal pPayments As List(Of String()))
    Dim x As Integer
    x += 1
End Sub

结果是pPayments是一个空列表,我尝试了pPayments as List(of string),结果相同。我还尝试使用四个属性创建一类支付,一个无参数构造函数和一个带参数的构造函数,我得到no parameterless constructor defined for this object。显然我不明白这个问题。有人有什么建议吗?

1 个答案:

答案 0 :(得分:0)

好的,我明白了。我改变了构建JSON对象的方式,而不是构造对象数组的数组数组:

jQuery(ajax)代码:

Public Class payments
    Public Sub New()
        MyClass.New("", "", "", "")
    End Sub
    Public Sub New(ByVal pPaymentdate As String, ByVal pAmount As String, ByVal pProcessingfee As String, ByVal pTotalamount As String)
        paymendate = pPaymentdate
        amount = pAmount
        processingfee = pProcessingfee
        totalamount = pTotalamount
    End Sub

    Public Property paymendate As String

    Public Property amount As String

    Public Property processingfee As String

    Public Property totalamount As String

End Class

});

付款类:

<WebMethod()> _
Public Shared Sub SaveGridViewData(ByVal pPayments As List(Of payments))
    ' Do Stuff
End Sub

网络方法:

subj.get_value('Tests').join(', ')