2

I am trying to pass an object to a page method defined. I'm trying to pass data to it collected from three textboxes.

Page Method

    [System.Web.Services.WebMethod]
    public static string saveDataToServer(object csObj)
    {
        // Function Body.
    }

Javascript/jQuery

        $("#osmSendMsg").click(function () {
            debugger;
            var cntDetails = {
                 cntName : $("#osmContactName").val(),
                 cntEmail : $("#osmContactEmail").val(),
                 cntMsg : $("#osmContactMessage").val()
            }
            PostDataToServer(cntDetails,"Please wait...","/ContactUs.aspx/saveDataToServer","csObj");
        });

PostDataToServer

// Post data to server (ajax call).
function PostDataToServer(dataToSend, strMessagetoShow, strMethodToCall, jsonObjectName) {
    debugger;
    /*
    dataToSend          Contains the JSON Object.
    submitType          1 == Normal Submit; 2 == Submit and Print.
    strMessagetoShow    Text that is displayed in the Please Wait Window.
    */
    var tempurl = strMethodToCall;
    var tempdata;
    $.ajax({
        url: tempurl,
        type: "POST",
        async: false,
        dataType: "json",
        data: "{" + jsonObjectName + ":" + JSON.stringify(dataToSend) + "}",
        //timeout: 30000,
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            tempdata = data;
        },
        error: function (result) {            
            tempdata = null;
        }
    });  //end of the ajax call
    return tempdata;
} //End of the post Data

Now the call is reaching the web method. No problem. I'm getting the object as well.But how do I process the object?

Data inside the object

As you can see, that's what I'm getting. I also tried declaring a class and passing it as the parameter..but all it's properties are empty. If you notice the data is appearing as a key, value pair. I could convert it into a Dictionary, but I believe that's a complicated solution.

Key - Value Pair

A simpler solution would be welcomed!

Abijeet Patro
  • 2,842
  • 4
  • 37
  • 64

2 Answers2

0

Your result collection is being returned in the 'data' parameter of your success method. You can simply process this parameter directly like data[0].cntName.

spadelives
  • 1,588
  • 13
  • 23
  • No, I'm trying to get the data on the server side, not the client side. Currently it's coming as a `object` type, but I am unable to process that object. – Abijeet Patro Mar 26 '13 at 20:10
0

As you are able to reach your webmethod, you need to modify your webmethod to read the data from the object as follows:

    [System.Web.Services.WebMethod]
    public static string saveDataToServer(Dictionary<string, string> csObj)
     {
            try
            {
                 string Name = csObj["cntName"].ToString();
                 string Email = csObj["cntEmail"].ToString();
                 //you can read values like this and can do your operation
                 return "";//return your value
            }
            catch(Exception ex)
            {
                  throw new Exception(Ex.Message);
            }
     }
Jeffrey Bosboom
  • 13,313
  • 16
  • 79
  • 92
ViKu
  • 235
  • 2
  • 14