微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – jQuery:如何遍历/迭代对象列表

我正在使用asp.net MVC4进行Web应用程序开发.

我想遍历viewmodel中的对象列表.

下面是对象的类:

public class User
{
        public int Id {get; set;}
        public string Name {get; set;}
        public string Address {get; set;}
        public string Department {get; set;}
}

下面是我的viewmodel类:

public class Userviewmodel
{
      public List<User> AllUsers {get; set;}
      public bool IsDeleted {get; set;}
}

如Userviewmodel类中所示,我有一个User类型的对象列表.现在我想使用Jquery迭代AllUsers列表中的每个用户对象并从中获取数据.

为了做到这一点,我尝试做了类似以下的事情:

$(@Model.AllUsers).each( function(){ .... });

我尝试过使用上述方法的不同组合,但无法成功.任何人都可以提出相同的解决方案.

提前致谢.

解决方法:

使用将您的集合分配给javascript变量

var users = @Html.Raw(Json.Encode(Model.AllUsers))

然后你可以迭代

$.each(users, function(index, item) {
  // access the properties of each user
  var id = item.Id;
  var name = item.Name;
  ....
});

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐