1

I have created two objects and want to pass them to UpdateJson.aspx.cs.

How can I retrieve the two objects on UpdateJson.aspx page?

I am getting an error on UpdateJson.aspx page.

function AddJSon() {
    var jsonOrderData = [];
    jsonOrderData.push({
        SalesOrderId: $("#<%=hdnOrderId.ClientID%>").val(),
        CustomerId: $("#<%=hdnCustomerId.ClientID%>").val(),
        SalesOrderDate: $("#cpContent_txtSalesOrderDate").val(),
        DeliveryDate: $("#cpContent_txtDeliveryDate").val(),
        GeneralDocument: $("#<%=hdnGeneratedDocumentName.ClientID%>").val(),
        OriginalGeneralDocument: $("#<%=hdnGeneratedDocumentName.ClientID%>").val(),
        TermsId: $("#<%=hdnTerm.ClientID%>").val(),
        CustomerPurchaseOrder: $("#cpContent_txtCusPurchaseOrder").val(),
        TrackingNo: $("#cpContent_txtTrackingNo").val(),
        SubTotal: $('#<%=lblSubTotal.ClientID%>').val(),
        Tax: $("#cpContent_txtTaxRate").val(),
        Shipping: $("#cpContent_txtShippingCharge").val(),
        NetTotal: $('#<%=lblNetTotal.ClientID%>').val(),
        ShippingAddressID: $("#<%=hdnshipping.ClientID%>").val(),
        ShippingCity: $("#cpContent_txtShippingCity").val(),
        ShippingCountry: $("#cpContent_txtShippingCountry").val(),
        ShippingState: $("#cpContent_txtShippingState").val(),
        ShippingStreet: $("#cpContent_txtShippingStreet1").val(),
        ShippingStreet2: $("#cpContent_txtShippingStreet2").val(),
        ShippingZip: $("#cpContent_txtShippingZip").val(),
        SalesOrderNo: $("#cpContent_txtSalesOrderNo").val(),
        OrderType: 0,
        IsSaveAndSend: $("#<%=hdnIsSaveAndSend.ClientID%>").val(),
        EmailIDList: $("#cpContent_txtMultipleEmailId").val(),
        ShippingAddress: $("#cpContent_txtShippingAddress").val(),
        Comment: $("#cpContent_txtComment").val(),
        IsConvert: true,
        IsRetailer: false,
    })

    var jsonProductDetail = [];

    $("#<%=grdOrderDetail.ClientID%> tr:has(td)").each(function () {
        var ProductCodeCheck = ($(this).find('.ProductCode').val());
        if (ProductCodeCheck != '') {
            jsonProductDetail.push({
                SrNo: $(this).find('.GridTextBox').val(),
                ProductId: $(this).find("#hdnEProductId").val(),
                ProductName: $(this).find("#txtProduct").val(),
                ProductCode: $(this).find('.ProductCode').val(),
                VendorId: $(this).find("#hdnEVendorId").val(),
                VendorName: $(this).find("#txtVendor").val(),
                Qty: $(this).find("#ddlQty").val(),
                itemRate: $(this).find("#txtItemRate").val(),
                SetUpCharge: $(this).find("#txtSetupCharges").val(),
                RunCharge: $(this).find("#txtRunCharge").val(),
                Price: $(this).find("#txtPrice").val(),
                ImprintMethodId: $(this).find("#hdnEImprintMethodId").val(),
                ImprintMethodChargeId: $(this).find("#hdnImprintMethodChargeId").val(),
                ImprintLocationId: 0,
                ImprintColorId:0,
                ImprintMethodName: $(this).find("#hdnImprintMethod").val,
                ImprintLocation: $(this).find("#hdnImprintLocation").val,
                ImprintColor:"",
                IsUrgentOrder:true,
                NeedBy:"",
            })
        }
    });

    $.ajax({
        url: "<%=Host %>Admin/UpdateJson.aspx?type=OrderData",
        type: 'POST',
        //data: JSON.stringify(jsonArr), { json_1:$.toJSON(data_1), json_2:$.toJSON(data_2) },
        data: { json_OrderData: JSON.stringify(jsonOrderData), json_OrderDetail: JSON.stringify(jsonProductDetail) },
        contentType: 'application/json; charset=utf-8',
        dataType: 'html',
        async: false,
        success: function (html) {
            Isresult = true;
        }
    })
}

I am using below code to retrieve the object on UpdateJson.aspx.cs page

StreamReader sr = new StreamReader(Request.InputStream);
            string strJson = "";
            strJson = sr.ReadToEnd();
            List<BusinessObject.SampleOrderBO.AttachmentData> returnList = new List<BusinessObject.SampleOrderBO.AttachmentData>();
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            strJson = "[" + strJson.Replace("[", "").Replace("]", "") + "]";
            if (strJson != "[]")
            {
                returnList = serializer.Deserialize<List<BusinessObject.SampleOrderBO.AttachmentData>>(strJson);
            }
jwpfox
  • 5,124
  • 11
  • 45
  • 42
grnake
  • 25
  • 6
  • You should use `data:JSON.stringify({ json_OrderData: jsonOrderData, json_OrderDetail: jsonProductDetail})` – Satpal Dec 08 '16 at 13:14
  • Or just give the object to `data`, as jQuery will stringify it for you – Rory McCrossan Dec 08 '16 at 13:15
  • Thanks i am able to pass data the problem is how can I extract the data on UpdateJson.aspx.cs page @Satpal – grnake Dec 08 '16 at 13:26
  • Create a class with property `json_OrderData` and `json_OrderDetail` and then use `serializer.Deserialize` – Satpal Dec 08 '16 at 13:27
  • var j = JArray.Parse(strJson); OrderData= JsonConvert.DeserializeObject>(j[1].ToString()); OrderDataDetail= JsonConvert.DeserializeObject>(j[2].ToString()); @Satpal is this the right way – grnake Dec 08 '16 at 13:30
  • Please don't use bold unnecessarily. What is the exact error message that you are seeing? – ChrisGPT was on strike Dec 08 '16 at 15:49
  • Try not to think in terms of "JSON objects". You have objects, and you want to send them to the server. So, just do that. Don't bother with `JSON.stringify` on the client at all; jQuery takes care of that. JSON is just the format of the data that's sent across the wire. – Heretic Monkey Dec 08 '16 at 15:53
  • Also, [the use of `async: false` is ... controversial](http://stackoverflow.com/q/24187160/215552), and indeed was always a bad idea as it could lock the UI. Be very careful when using this in production code. – Heretic Monkey Dec 08 '16 at 15:57
  • @MikeMcCaughan Have send it to server the problem is how should i retrieve them. – grnake Dec 09 '16 at 06:15

0 Answers0