最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery: How to traverseIterate over a list of object - Stack Overflow

programmeradmin0浏览0评论

I'm using asp MVC4 for web app development.

I would like to traverse a list of objects from a ViewModel.

Below is the class of the object:

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

Below is my ViewModel class:

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

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.

Thanks in advance.

I'm using asp.net MVC4 for web app development.

I would like to traverse a list of objects from a ViewModel.

Below is the class of the object:

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

Below is my ViewModel class:

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

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.

Thanks in advance.

Share Improve this question asked Jun 9, 2015 at 10:56 ZaxZax 3,0107 gold badges56 silver badges78 bronze badges 8
  • What error do you get? Did you try using $.each() function in that way: $.each(@Model.AllUsers, function() {}); ? – SzybkiSasza Commented Jun 9, 2015 at 10:58
  • if you wanna do a normal looping @foreach(var user in @Model.AllUsers) {//try } . cheers – super cool Commented Jun 9, 2015 at 11:00
  • Syntax errors most of the times – Zax Commented Jun 9, 2015 at 11:00
  • @supercool: If i use that approach i cannot access any variables outside the for each loop. In my casei need to perform some conditional checks. – Zax Commented Jun 9, 2015 at 11:02
  • is it ? i believe you can access . $.each or @foreach whatever EOD all do the same thing . if you want to access outside variables you can easily access them via @ notation i.e @if(@status == false) inside loop . – super cool Commented Jun 9, 2015 at 11:13
 |  Show 3 more comments

4 Answers 4

Reset to default 14

Assign your collection to a javascript variable using

var users = @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;
  ....
});
<script type="text/javascript">

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

    for (var i = 0; i < UsersList.length; i++) {
        alert(UsersList[i].Id);
        alert(UsersList[i].Name);

    } 
 </script>

JavaScript generally is unhappy with razor components although if the above is part of an CSHTML file it will work.

The other approaches are:

  1. Display the collection using razor @foreach ...
  2. Pass the collection as a parameter from you webpage into a JavaScript function on some event

How are you calling this function and what does it do?

In My Case, I fixed by this way :

 @for(int i=0;i<Model.AllUsers.Count;i++)
 {
   @: var name = '@Html.Raw(@Model.AllUsers[i].Name)';
   @:alert(name);
 }
发布评论

评论列表(0)

  1. 暂无评论