I am attempting to send my client-side custom JavaScript object to an ASP.net Web Method using jQuery Ajax. Below is an example of the object I am working with:
function Customer() {
this.Name = "";
this.Surname = "";
this.Addresses = new Array();
}
I gather data using the following method:
function buildCurrentCustomer() {
var currentCustomer = new Customer();
/** General Info **/
currentCustomer.Name = $("#Name").val();
currentCustomer.Surname = $("#Surname").val();
currentCustomer.Addresses = new Array();
currentCustomer.Addresses["HOME"] = $("#adHome").val();
currentCustomer.Addresses["OFFICE"] = $("#adOffice").val();
return currentCustomer;
}
To send this data, I use the following code:
$.ajax({
type: "POST",
url: "../_layouts/CustomerManager/MasterPage.aspx/SetCustomer",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: "{customer: " + JSON.stringify(currentCustomer) + "}",
cache: false,
success: function (result) {
},
error: function (ex) {
WriteToConsole(ex.responseText);
}
});
The server-side method I am calling looks like this:
[WebMethod]
public static bool SetCustomer(CustomerModel Customer)
{
//My code...
}
And here is the CustomerModel class used in the server-side method:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Common.Model.JavaScriptModel
{
public class CustomerModel
{
/** General Info **/
public string Name {get;set;}
public string Surname {get;set;}
public Dictionary<string, string> Addresses { get; set; }
}
}
However, I am encountering an issue where the server-side method does not execute properly when called through Ajax. If I adjust the signature of the server-side method to accept a List instead:
public static bool SetCustomer(List<CustomerModel> Customer)
The SetCustomer method is then executed, but the List remains empty. Can anyone provide insight into why I might be experiencing this problem and where I can find documentation on how to resolve it?
Thank you.