0

I use the Handlebar/Mustache template to render the following view model in my ASP.NET MVC app. I am displaying the following fields in a table. The ContactList is a list of Contacts (with FirstName, LastName, Address etc.).

public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }       
public List<Contact> ContactList { get; set; }



<table
    <tbody>                         
                    <tr>        
                        <td>First Name</td>                    
                        <td>{{this.FirstName}}</td>
                    </tr>   
                    <tr>    
                        <td>Last Name</td>                   
                        <td>{{this.LastName}}</td>
                    </tr>
                    <tr>        
                        <td>Address</td>                           
                        <td>{{this.Address}}</td>
                    </tr>   
    <tr>    
                        <td>Contact List</td>                   
                        **<td>//how to process the ContactList?//</td>**
                    </tr>
</tbody>                                  
                </table>

I would like to know how to loop through this list and display it as a row in the same table?

dotNetNewbie
  • 719
  • 4
  • 15
  • 33

1 Answers1

1

From handlebars site:

You can iterate over a list using the built-in each helper. Inside the block, you can use this to reference the element being iterated over.

<ul class="people_list">
  {{#each people}}
  <li>{{this}}</li>
  {{/each}}
</ul>

when used with this context:

{
  people: [
    "Yehuda Katz",
    "Alan Johnson",
    "Charles Jolley"
  ]
}

So I believe you just need to have your ContactList in a format HandleBars can understand (an array as an object property).

montrealist
  • 5,241
  • 10
  • 43
  • 57
  • Here's a similar question as well: http://stackoverflow.com/questions/9058774/handlebars-mustache-is-there-a-built-in-way-to-loop-through-the-properties-of – montrealist Oct 24 '12 at 15:11