Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Torben 3 posts 95 karma points
    Aug 19, 2018 @ 05:41
    Torben
    0

    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")

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
        @{
            Layout = "CSSgrid.cshtml";
            var childPages = CurrentPage.Children();
        }
        <main>
            @foreach (var category in childPages)
            {
                var affiliateItems = category.GetPropertyValue<IEnumerable<IPublishedContent>>("affiliateItems");
            <section>
                <article>
    
                    <img src="@Umbraco.Media(category.categoryImage.Id).GetCropUrl("square") " alt="@category.headline">
                </article>
            </section>
            <ul>
                @foreach (var item in affiliateItems)
                    {
                <li>
                    @item.Name
                    @Umbraco.Field(item, "text")
                </li>
                    }
            </ul>
                }
        </main>
    
  • Ben Palmer 176 posts 842 karma points c-trib
    Aug 19, 2018 @ 08:18
    Ben Palmer
    2

    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:

    @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>
    }
    

    Hope that helps,

    Ben

  • Torben 3 posts 95 karma points
    Aug 21, 2018 @ 19:12
    Torben
    0

    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:

    Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type

  • Ben Palmer 176 posts 842 karma points c-trib
    Aug 22, 2018 @ 09:22
    Ben Palmer
    0

    This may be because you're using dynamics.

    Try replacing var childPages = CurrentPage.Children(); with var childPages = Model.Content.Children;

    If that doesn't work, could you post your code again with the updates in place?

  • Torben 3 posts 95 karma points
    Aug 26, 2018 @ 14:21
    Torben
    102

    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>
    
Please Sign in or register to post replies

Write your reply to:

Draft