0

I'm going crazy. Maybe someone can help.

I have my ajax on the website passing data to the asmx on the backend.

the data is as so. and it's valid json. http://jsonlint.com/

{ "data": [{"Address":"addasdf","City":"casdf","County":"casdf","State":"dc","Zip":"33333","ReferenceID":"asdf","LegalDescription":"asdf","PreviousOwner":"asdf","ClientNotes":"sadf","AcquisitionType":"sadf","ClientAcquisitionDate":"asdf","ForeclosureDate":"asdf"}]}

The ajax sending the data is here. And as far as I can tell this is sending correctly and is right.

$.ajax({
        type: "POST",
        url: "/ServiceFolder/BulkOrder.asmx/insertBulk",
        data: "{ \"data\": " + smyData + "}",              
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data, textStatus, jqXHR) { //
            //alert(data);
            console.log(data);
        },
        error: function (xmlHttpRequest, textStatus, errorThrown) {
            console.log(xmlHttpRequest.responseText);
            console.log(textStatus);
            console.log(errorThrown);
            alert("Screen shot this error getEntityMarkers: " + xmlHttpRequest.toString() + " " + textStatus.toString() + " " + errorThrown.toString());
        }
    });

I have my class that models the data being sent.

[Serializable]
public class BulkOrderObject
{
    public List<BulkOrderObjectData> data { get; set; }
}

[Serializable]
public class BulkOrderObjectData
{
    public string Address { get; set; }
    public string City { get; set; }
    public string County { get; set; }
    public string State { get; set; }
    public string Zip { get; set; }
    public string ReferenceID { get; set; }
    public string LegalDescription { get; set; }
    public string PreviousOwner { get; set; }
    public string ClientNotes { get; set; }
    public string AcquisitionType { get; set; }
    public string ClientAcquisitionDate { get; set; }
    public string ForeclosureDate { get; set; }    
}

And finally where I think my problems lie in the webmethod.

[WebMethod]
        public string insertBulk(List<BulkOrderObject> data)
        {

            string myreturn = String.Empty;
            myreturn += data.Count;  // data returns count

            //List<BulkOrderObject> l = data as List<BulkOrderObject>;

            try
            {
                foreach (var i in data)
                {
                    myreturn += i.data.Count;  // error here.
                }
            }
            catch (Exception ex)
            {
                myreturn += ex.ToString();
            }

            return myreturn;
        }

something funky is going on here at public string insertBulk(List< BulkOrderObject > data) when I try to get the number of entries in data.count it shows that there's 1. however when I try to foreach the data in data it returns an error.

    {"d":"1System.NullReferenceException: Object reference not set to an instance of an object.\r\n   at
 test.insertBulk(List`1 data)"} 

It's as if it's not filling up the BulkOrderObjectData.

Any thoughts?

Prescient
  • 1,051
  • 3
  • 17
  • 42
  • Try here: http://www.aspsnippets.com/Articles/Calling-ASPNet-WebMethod-using-jQuery-AJAX.aspx or here http://stackoverflow.com/questions/7770679/jquery-ajax-call-to-an-asp-net-webmethod. Is `smyData` properly stringified? – dbc May 15 '15 at 19:32
  • 1
    Thanks but that wasn't it at all. The issue was my passing the data to the webmethod. I had to change my wrapper object from data to BulkOrderObjectData. then in my webmethod use List BulkOrderObjectData. After that it worked perfectly. – Prescient May 15 '15 at 20:46

0 Answers0