title: Javascript: How to iterate through list of objects in Model
body: <p>so I need to get to fetch the names of students in a list of student object that is in a view's model then send them to the server via $.post, the latter I have figured it out but I can't figure out how to iterate through the list of objects. Basically I have this:</p>
  
  <pre><code>//Student object
  public class Student 
  {
      public string FirstName { get; set; }
      public string LastName { get; set; }
      //Like a bunch of other attributes here
  } 
  </code></pre>
  
  <p>This is the model in the view:</p>
  
  <pre><code>//StudentSearchResult ViewModel
  
  public class StudentSearchResult {
  
      public IEnumerable&lt;Student&gt; { get; set;}
  }
  </code></pre>
  
  <p>At first I though of just sending the student list object as is, but it may not be a good idea as it is bundled with too many attributes (it gave me this 'circular' error when I tried to send the model) and I only really need to send the concatenated FirstName and LastName to the controller using the $.post method I already have. I tried things like these:</p>
  
  <pre><code>var names = []  
  var length = "@Model.StudentSearchResult.count()";  
  for (int i = 0; i &lt; length; i++) 
  {
       names[] = "@Model.StudentSearchResult[i].Name + @Model.StudentSearchResult[i].LastName"
  }
  //$.post function here that calls the controller and send the concatenated names of each student in studentsearchresult.
  </code></pre>
  
  <p>But I'd get an error that "i" doesn't exists so, how can I iterate in javascript through the list of objects in my view model, access the atributes and concatenate them and then store them on an array of strings so that I may send it to the controller? I imagine the controller would look like this</p>
  
  <pre><code>[HttpPost]
  public ActionResult StudentSearchResult(/*other stuff I send here, */ string[] studentNames){
     //stuff here
  
    return View();
  }
  </code></pre>
  
  <p>Thanks!</p>