遍历Javascript中的模型列表

时间:2018-11-01 15:21:39

标签: javascript

我有两个班:人员和帐户。我在ASP网络中工作。

这是我的控制者:

        public List<Person> GetPeople()
        {
        using (var db = new PeopleContext())
        {
            return db.People.ToList();
        }
        }

人员具有以下属性:

    public class Person
    {
    public int PersonId{ get; set; }

    public string Name{ get; set; }

    public List<Accounts> Accounts{ get; set; }
    }

帐户类包含以下属性:

    public class Account
    {
    public int AccoundId{ get; set; }

    public string AccountName{ get; set; }

    public Person Person{ get; set; }
    }

这是我的看法

    function getQuestions() {
    $.ajax({
        url: '/api/Person',
        type: 'GET',
        //dataType: 'json',
        traditional: true,
        success: function (data) {
        $.each(data, function (i, p) {
        string += '<h1>Name: '+p.Name+'</h1>';
        $.each(p.Accounts, function (j, a) {
            string += '<p>'+a.AccountName+'</p>';
        });
    });
   },
 });

如何遍历JavaScript中的帐户列表并在我的视图中显示?我一直在尝试这样做,但是列表返回null。当我尝试管理日志数据时,它将Accounts显示为null。我该如何解决?

1 个答案:

答案 0 :(得分:0)

我相信您需要显示/ api / Faq网络服务响应。

我还建议您打开F12开发人员工具,并在获得名称后输入debugger一词:

string += '<h1>Name: '+p.Name+'</h1>';
debugger;

然后,您可以查看p.Accounts对象,看看它是什么。

如果Accounts对象为空,则可能需要添加到Person对象的构造函数中:

public Person()
        {
            Accounts= new HashSet<Account>();
        }