Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
297 views
in Technique[技术] by (71.8m points)

asp.net - How to pass Generic List from code behind to javascript

Is it possible to pass the generic list of addresses to client side in java script?

List data is availble at server side, i want to send the list to java script function which will get geocoding for all addresses and then return the list to server side.

  • User clicks the search
  • On Page Post back, list is generated.
  • Before results are shown on the page, call the java script function and loop through the list and get the geocodes and update the list and return the results to server.
  • Show the results on page.

Here what i have tried so far, don't know how to read list in javascript function and then return the results to server.

 Private Shared Function CreateGenericArray() As List(Of AddressInfo)
    Dim _AddressInfo As New List(Of AddressInfo)()
    Dim lp As New AddressInfo()
    lp.AddressID = 1
    lp.AddressLine1 = "My Address"
    lp.City = "PA"
    lp.PostalCode = "11654"
    _AddressInfo.Add(lp)

    Return _AddressInfo
End Function

    Public Sub ConvertToJSON()
     Dim jss1 As New JavaScriptSerializer()
     Dim _myJSONstring As String = jss1.Serialize(CreateGenericArray())
     Dim player As String = (Convert.ToString("var player=") & _myJSONstring) + ";"
     Page.ClientScript.RegisterClientScriptBlock(Me.[GetType](), "player123", player, True)       
End Sub



<form id="form1" runat="server">

    <script type="text/javascript">
        $(player).each(function (index, person) {
            alert('AddressID: ' + person.AddressID +
          ' AddressLine1: ' + person.AddressLine1 +
          ' City: ' + person.City
        );
    });
    </script>
</form>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...