3

I have a data template called ProductListingPage. I also have a data template called Product. All of the Products will be in the content tree directly beneath the ProductListingPage. I am using automatic code generation with TDS and Glass Mapper. Is there a way that I can specify that the children of the ProductListingPage should be of type Product? Can I specify that somewhere so that when the code is generated for the ProductListingPage type it will have that property?

Ideally in my razor view I would like to be able to loop through all of the products like this:

@foreach(var product in Model.Products)
{
  Display the product info here...
}

1 Answer 1

5

You can't do that using the out of the box T4 templates. Typically I would handle this by creating a new partial class that includes a property for the children. Something like this below:

public partial class ProductListingPage
{
    [SitecoreChildren]
    public IEnumerable<Product> Products { get; set; }
}

This would be combined with the partial class that's generated by the T4 template for your ProductListingPage model. You would define this partial in a separate file, such as ProductListingPage.partial.cs depending on your naming convention. It should be placed in the exact same namespace as the generated code, so that the two partials are combined at runtime.

So in your code you should be able to do something like this:

@foreach(var product in Model.Products)
{
    @product.Title
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.