Documentation needed for simple category-child listing using razor templates

May 12, 2015 at 4:21 PM
As per Daniel Mettler's suggestion on Twitter, I'm re-posting my question here about categories.

I haven't fully figured out how to setup and display a list of categories and the items in each category. From a content editors point of view, they would have 1 module and 1 list. Then each item in the list would have a Category dropdown to set the category.

Here's what the outputted code might look like:
<h2>Category One</h2>
<li>Hobby 1</li>
<li>Second hobby</h3>
<li>Hobby #3</li>

<h2>Category Two</h2>
<li>Fourth hobby</li>
<li>Hobby Cinq</h3>
<li>The Sixth Hobby</li>

<h2>Category Three</h2>
<li>Seventh hobby</li>
<li>Hobby Eight</h3>
<li>The ninth and last hobby</li>
So, please provide these:
  1. How to set up the content types and entities
  2. Please provide a working razor template
May 12, 2015 at 5:42 PM
Hey Aaron

Here is my 3 minutes of live setup and coding

setting up very simple person with hobbies app

as well as the app created there

download hobbies app

I leave it to Daniel to make an example using the new visual designer and the RelationshipDataSource, have to look into that some more detail myself as well.

Armand 2DNN
Your friendly non official 2sxc support :)
Marked as answer by dnnsuperfan on 5/12/2015 at 1:51 PM
May 12, 2015 at 8:52 PM
This looks fantastic. It really helps me understand a lot better. Thank you.
May 13, 2015 at 6:01 AM
My 2cents

Great solution - many thanks for that video :) !

You still have 2 core questions: are you creating a list of Categories - with hobbies attached - or a list of hobbies, with categories attached.

Depending on your goal the setup is a bit different. nokikos example created a categories list with hobbies attached.

If your primary goal is to create hobbies and give them one or more categories, then your setup is a bit different

Content type Hobby
  • Title
  • Description
  • Category
The code can be done in many ways. Beginners usually various loops and always check if the category changed (if lastCategory!= currentCategory) to then add a header in between. The cleanest is to prepare the data (sort/group) and then just output it. To do that you need some LINQ-know how, so if you don't understand that you'll have to decide if it's worth learning (should take you a few hours) but trust me - it's absolutely worth it!
Here something about grouping:

Template (pseudo-code, I didn't check the details)
using System.Linq
// for remembering current hobby category

var hobbies = AsDynamic(Data["Default"]);
var hobbyGroups = from h in hobbies
    group h by h.Category[0].Name into hobbyGroup
    select hobbyGroup

// var lastCategory = "";
@foreach(var group in hobbyGroup) {
@foreach(var hob in group) {

May 13, 2015 at 4:35 PM
I'm actually building an FAQ section. So it's a list of questions. But I want them sorted by category.

I'll take a look at this example you provided. Thanks.