Copied to clipboard

Flag this post as spam?

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


  • Stephen 94 posts 255 karma points
    Dec 22, 2015 @ 02:54
    Stephen
    0

    OK so this is weird, can someone explain

    Hi All,

    So it's fixed, but its a really stupid fix I found by accident, and as best I know it shouldn't work. And this took hour to find.

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @{
    if (!String.IsNullOrEmpty(Model.MacroParameters["newsRoot"].ToString())) {
        dynamic node = @Umbraco.Content(Model.MacroParameters["newsRoot"]).Children;    
        <div></div>
        foreach (var newss in node.Take(1).OrderBy("createDate desc")) {
            <a href="@Umbraco.Content(newss.Id).Url" alt="@newss.pageTitle">
                <div class="col2NewsA">
                    <h3>@newss.pageTitle</h3>           
                    <span><p>@newss.teaserText</p></span>
                    <img src="@newss.GetCropUrl("newsImage", "newsTile")" alt="@newss.pageTitle image" />
                </div>
            </a>        
        }
    }
    }
    

    OK, so this is for "Partial View Marco Files" (not a partial view).

    You see the line 5 down, "div /div".

    It won't work without that. I have had problems with foreach before inside if's but not like this.

    As far as I am concerned if the "div /div" is in there the "foreach" should start with an @, right?? Put the @ in and it's broken again.

    What is happening, are there some mad special rules for foreach?

    Thanks

    Stephen

  • James Jackson-South 489 posts 1747 karma points c-trib
    Dec 22, 2015 @ 06:03
    James Jackson-South
    101

    Purely a guess but try removing the @ symbol on this line.

    dynamic node = @Umbraco.Content(Model.MacroParameters["newsRoot"]).Children;  
    

    You're still in a code block there so you shouldn't need to use it.

  • Stephen 94 posts 255 karma points
    Dec 22, 2015 @ 11:16
    Stephen
    0

    Thanks James, that was what was causing it. That's the problem with coding late at night, you miss the obvious.

  • Patrick Hansen 13 posts 83 karma points
    Dec 22, 2015 @ 08:38
    Patrick Hansen
    0
    <div></div>
    foreach (var newss in node.Take(1).OrderBy("createDate desc")) 
    

    You are closing the div tags before the foreach loop, so you're still in the code block.

    If you had done something like:

    <div>
        foreach (var newss in node.Take(1).OrderBy("createDate desc"))
        {
        <a href="@Umbraco.Content(newss.Id).Url" alt="@newss.pageTitle">
            <div class="col2NewsA">
                <h3>@newss.pageTitle</h3>
                <span><p>@newss.teaserText</p></span>
                <img src="@newss.GetCropUrl("newsImage", "newsTile")" alt="@newss.pageTitle image" />
            </div>
        </a>
        }
    </div>
    

    Where you write code inside the div tags, then you'd have to use "@" infront of the Foreach loop.

    James is also right, the "@" is not needed on:

    dynamic node = Umbraco.Content(Model.MacroParameters["newsRoot"]).Children;
    

    Since you are inside the code block

    This should be the code without the empty div tags:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @{
        if (!String.IsNullOrEmpty(Model.MacroParameters["newsRoot"].ToString()))
        {
            dynamic node = Umbraco.Content(Model.MacroParameters["newsRoot"]).Children;
            foreach (var newss in node.Take(1).OrderBy("createDate desc"))
            {
                <a href="@Umbraco.Content(newss.Id).Url" alt="@newss.pageTitle">
                    <div class="col2NewsA">
                        <h3>@newss.pageTitle</h3>
                        <span><p>@newss.teaserText</p></span>
                        <img src="@newss.GetCropUrl("newsImage", "newsTile")" alt="@newss.pageTitle image" />
                    </div>
                </a>
            }
        }
    }
    
  • Stephen 94 posts 255 karma points
    Dec 22, 2015 @ 11:17
    Stephen
    0

    Thanks for the info, yes James was right, that was the problem.

  • James Jackson-South 489 posts 1747 karma points c-trib
    Dec 22, 2015 @ 12:33
    James Jackson-South
    1

    Great stuff. Glad I could help!

Please Sign in or register to post replies

Write your reply to:

Draft