jQuery:如何遍历/迭代对象列表 [英] jQuery: How to traverse / Iterate over a list of object
问题描述
我使用 asp.net MVC4 进行网络应用开发.
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;}
}
下面是我的 ViewModel
类:
public class UserViewModel
{
public List<User> AllUsers {get; set;}
public bool IsDeleted {get; set;}
}
正如在 UserViewModel
类中所见,我有一个 User
类型的对象列表.现在我想使用 Jquery
遍历 AllUsers
列表中的每个用户对象并从中获取数据.
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.
提前致谢.
推荐答案
使用
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;
....
});
这篇关于jQuery:如何遍历/迭代对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!