I'm trying to display a list with details which I have created in my code behind. But I am getting undefined in the console. Why does that happen?
我正在尝试显示一个列表,其中包含我在后面的代码中创建的详细信息。但我在控制台中未定义。为什么会这样?
cs:
CS:
public partial class WebForm1 : System.Web.UI.Page
{
public List<Person> persons { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
Person p1 = new Person() { name = "Tom" };
Person p2 = new Person() { name = "Ed" };
List<Person> lijst = new List<Person>();
lijst.Add(p1);
lijst.Add(p2);
persons = lijst;
}
}
public class Person
{
public string name { get; set; }
}
aspx:
ASPX:
<script>
var persons = '<%= persons%>'
var length = persons.length;
for (var i = 0; i < length; i++) {
person = persons[i];
console.log(person.name);
}
</script>
2 个解决方案
#1
1
Problem is javascript not having idea about your C# class and it's properties. You may need to convert or serialize it to a known type like string, xml or json etc..
问题是javascript对你的C#类及其属性没有了解。您可能需要将其转换或序列化为已知类型,如字符串,xml或json等。
try something like below
尝试类似下面的东西
You can have method return comma separated person names from class property
您可以从类属性中使用方法返回逗号分隔的人名
public string GetPersonNames
{
get
{
return string.Join(",", persons.Select(p => p.name));
}
}
in your aspx you can get person names and split by '
在您的aspx中,您可以获取人名并将其拆分为'
<script>
var persons = '<%= GetPersonNames%>';
</script>
Or try below
或者尝试以下
<script type="text/javascript">
var persons = '<%= new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(persons)%>';
persons = JSON.parse(persons);
var length = persons.length;
alert(length);
for (var i = 0; i < length; i++) {
var person = persons[i];
alert(person.name);
}
</script>
#2
0
Try this, code in .cs
试试这个,代码在.cs
Person p1 = new Person() { name = "Tom" };
Person p2 = new Person() { name = "Ed" };
List<Person> lijst = new List<Person>();
lijst.Add(p1);
lijst.Add(p2);
persons = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(lijst);
javascript in aspx
aspx中的javascript
var persons = eval('<%= persons%>');
//this will show "Tom"
alert(persons[0].name);
#1
1
Problem is javascript not having idea about your C# class and it's properties. You may need to convert or serialize it to a known type like string, xml or json etc..
问题是javascript对你的C#类及其属性没有了解。您可能需要将其转换或序列化为已知类型,如字符串,xml或json等。
try something like below
尝试类似下面的东西
You can have method return comma separated person names from class property
您可以从类属性中使用方法返回逗号分隔的人名
public string GetPersonNames
{
get
{
return string.Join(",", persons.Select(p => p.name));
}
}
in your aspx you can get person names and split by '
在您的aspx中,您可以获取人名并将其拆分为'
<script>
var persons = '<%= GetPersonNames%>';
</script>
Or try below
或者尝试以下
<script type="text/javascript">
var persons = '<%= new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(persons)%>';
persons = JSON.parse(persons);
var length = persons.length;
alert(length);
for (var i = 0; i < length; i++) {
var person = persons[i];
alert(person.name);
}
</script>
#2
0
Try this, code in .cs
试试这个,代码在.cs
Person p1 = new Person() { name = "Tom" };
Person p2 = new Person() { name = "Ed" };
List<Person> lijst = new List<Person>();
lijst.Add(p1);
lijst.Add(p2);
persons = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(lijst);
javascript in aspx
aspx中的javascript
var persons = eval('<%= persons%>');
//this will show "Tom"
alert(persons[0].name);