jQuery的:如何穿越/遍历对象的列表 [英] jQuery: How to traverse / Iterate over a list of object
问题描述
我使用asp.net MVC4的Web应用开发。
I'm using asp.net MVC4 for web app development.
我想从一个ViewModel遍历对象的列表。
I would like to traverse a list of objects from a ViewModel.
下面是一个类的对象:
public class User
{
public int Id {get; set;}
public string Name {get; set;}
public string Address {get; set;}
public string Department {get; set;}
}
下面是我的视图模型
类:
public class UserViewModel
{
public List<User> AllUsers {get; set;}
public bool IsDeleted {get; set;}
}
正如 UserViewModel
类看出,我有类型的对象的列表用户
。现在我想通过每个用户对象的 AllUsers的
来重复使用 Jquery的
,并且可以从他们的数据列表。
As seen in the UserViewModel
class, I have a list of objects of type User
. Now i would like to iterate through each of the user object in AllUsers
list using Jquery
and fetch data from them.
在为了做到这一点,我试图做类似如下:
In order to do so, I tried doing something like the following:
$(@Model.AllUsers).each( function(){ .... });
我已经用上面的方法尝试了不同的组合,但未能成功。任何人都可以提出一个相同的解决方案。
I have tried different combination using the above approach, but couldn't succeed. Can anyone suggest a solution for the same.
先谢谢了。
推荐答案
使用指定的集合,一个JavaScript变量
Assign your collection to a javascript variable using
var users = JSON.parse('@Html.Raw(Json.Encode(Model.AllUsers))');
然后你就可以遍历
which you can then iterate over
$.each(users, function(index, item) {
// access the properties of each user
var id = item.Id;
var name = item.Name;
....
});
这篇关于jQuery的:如何穿越/遍历对象的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!