[Solved] How to pass Generic List from code behind to javascript


I’ll be utilizing C# rather than Visual Basic, but you could essentially do this:

Code Behind:

JavaScriptSerializer serializer = new JavaScriptSerializer();
List<Address> deserialize = serializer.Deserialize<List<Address>>(address);
foreach(Address address in deserialize)
{
     // Do something with Exposed Properties:
}

The Address Class will be very, very basic:

public class Address
{
     public int Id { get; set; }
     public string Street { get; set; }
     public string City { get; set; }
     public string State { get; set; }
     public string Zip { get; set; }
}

That is essentially the backend, now all you have to do on the front-end is:

function BuildAddress(Id, Street, City, State, Zip) {
     var address = null;
     item = {
          Id: Id,
          Street: Street,
          City: City,
          State: State,
          Zip: Zip
     };
}

A clean function to build our object. Now, we actually need to pass that content:

var address = new Array();
var convertAddress;

address.push(BuildAddress(id, street, city, state, zip));
convertedAddress = JSON.stringfy(address);

$.ajax({
     url: '<%= Page.ResolveUrl("~/Services/Location.aspx") %>'
     data: { Address: convertedAddress},
     type: 'POST',

     success: function (address) {
          var result = JSON.parse(address);
          // Do something with result, example: result[0].City
     }
});

That will pass the data in the manner your attempting. You’ll have to play with it a bit though.

2

solved How to pass Generic List from code behind to javascript