What version of Umbraco are you using? Both should be entirely possible by making use of the Models Builder.
I tend to use a Select in my foreach to set the IPublishedContent up as models which would allow you to do something such as this:
@foreach (Category category in childPages.Select(x => new Category(x))
{
@category.CategoryImage.GetCropUrl("square");
}
Category would be a model which is generated by the models builder and you'll probably want to enable property value converters as well if they're not already.
You can then do the same thing with your second loop:
@foreach (AffiliateItem item in affiliateItems.Select(x => new AffiliateItem(x)))
{
<li>
@item.Name
@item.Text
</li>
}
I read Models Builder multiple times and also watched the Models Builder videos on umbraco TV. I was able to rewrite my code. And got things to work and my code looks much better now. Once again thanks for your help.
@inherits UmbracoViewPage<AffiliatePage>
@using Umbraco.Web.PublishedContentModels;
@{
Layout = "blogCSSgrid.cshtml";
}
<main>
@foreach (var category in Model.Children<AffiliatePageCategory>())
{
<section>
<article>
<h1>@category.Headline</h1>
<p>@category.CategoryIntroText</p>
<img src="@category.CategoryImage.GetCropUrl("square") " alt="@category.Headline">
</article>
</section>
<ul>
@* This is Umbraco.MultiNodeTreePicker2 - category.AffiliateItems<AffiliateAD> is not possible*@
@foreach (var item in category.AffiliateItems)
{
<li>
@* would be more readable with item.link *@
<a href="@item.GetPropertyValue("link")" target="_blank"> <img src="@item.GetPropertyValue("image")"></a>
@if (item.HasValue("blogPost"))
{
@* not nice but working *@
<a href="@Umbraco.TypedContent(item.GetPropertyValue("blogPost")).Url">My blog post</a>
}
</li>
}
</ul>
}
</main>
Notation improvements: GetCropUrl & nested foreach
A more intuitive way of writing the code would be if I just could write:
category.categoryImage.GetCropUrl("square")
like I could write: category.categoryImage.Url
In addition in the nested foreach I would rather write:
@item.text instead of @Umbraco.Field(item, "text")
Hi Torben,
What version of Umbraco are you using? Both should be entirely possible by making use of the Models Builder.
I tend to use a
Select
in my foreach to set the IPublishedContent up as models which would allow you to do something such as this:Category
would be a model which is generated by the models builder and you'll probably want to enable property value converters as well if they're not already.You can then do the same thing with your second loop:
Hope that helps,
Ben
I am on Umbraco 7.12
I like the idea. This is exactly what I was looking for however trying this I get an error:
This may be because you're using dynamics.
Try replacing
var childPages = CurrentPage.Children();
withvar childPages = Model.Content.Children;
If that doesn't work, could you post your code again with the updates in place?
I read Models Builder multiple times and also watched the Models Builder videos on umbraco TV. I was able to rewrite my code. And got things to work and my code looks much better now. Once again thanks for your help.
is working on a reply...