NickP
NickP

Reputation: 357

Nesting a @ foreach in Webmatrix 2

I'm a tad rusty here so bear with me. I'm trying to get my list of items within their groups like so.

  <h2>Category Heading 1</h2>
   <ul>
     <li>Item list 1</li>
    <li>Item list 2</li>
   </ul>


  <h2>Category Heading 2</h2>
   <ul>
     <li>Item list 1</li>
    <li>Item list 2</li>
   </ul>

And my code is - I'm getting errors

 @foreach (var group in db.Query(GroupName)){
    <h3>@group.Name</h3>
<ul>
    @foreach(var row in db.Query(queryList)){
  <li><a href="/[email protected]"> @row.title</a></li> 
  }
 </ul>
}

Any help would be appreciated

Upvotes: 1

Views: 271

Answers (1)

Mike Brind
Mike Brind

Reputation: 30110

You should be able to do this by getting all the data in one go and using the LINQ GroupBy operator. Here is an article I wrote about displaying hierarchical data like this: http://www.mikesdotnetting.com/Article/189/Efficiently-Displaying-Hierarchical-Data-With-The-jQuery-Accordion-In-Razor-Web-Pages

You can ignore the 2nd half where I introduce the jQuery accordion...

Upvotes: 1

Related Questions