如何将通用列表从后面的代码传递给 javascript [英] How to pass Generic List from code behind to javascript
问题描述
是否可以在java脚本中将通用地址列表传递给客户端?
Is it possible to pass the generic list of addresses to client side in java script?
列表数据在服务器端可用,我想将列表发送到 java 脚本函数,该函数将对所有地址进行地理编码,然后将列表返回到服务器端.
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.
- 用户点击搜索
- 在页面回发时,生成列表.
- 在页面上显示结果之前,调用 java 脚本函数并遍历列表并获取地理编码并更新列表并将结果返回到服务器.
- 在页面上显示结果.
到目前为止我已经尝试过,不知道如何在javascript函数中读取列表,然后将结果返回给服务器.
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>
推荐答案
我将使用 C#
而不是 Visual Basic,但您基本上可以这样做:
I'll be utilizing C#
rather than Visual Basic, but you could essentially do this:
背后的代码:
JavaScriptSerializer serializer = new JavaScriptSerializer();
List<Address> deserialize = serializer.Deserialize<List<Address>>(address);
foreach(Address address in deserialize)
{
// Do something with Exposed Properties:
}
Address Class
将非常非常基础:
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.
这篇关于如何将通用列表从后面的代码传递给 javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!