Copied to clipboard

Flag this post as spam?

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


  • Jay 49 posts 211 karma points
    Dec 23, 2014 @ 13:01
    Jay
    0

    How To Iterate Folder's Children Using Linq In Razor View?

    Hi all,

    I am currently trying to iterate a folder's children by using linq in a razor view, in a similar way to this example.

    My own setup is slightly different from the one in the example. The example uses a partial view, whereas I'm using a partial macro view. I figured it wouldn't make any difference, and Umbraco's documentation states that partial macro views are the preferred way, so...

    Anyway... my razor view throws errors when I'm trying to call linq methods such as ToList, Select and Where.

    See screenshot below.

    The intellisense clearly shows that the Children property is a 'where select linq enumerator'. So why is this not working?

    Also... why is VS.NET showing so many squiggly lines, even on the lines of code that are, in fact, correct and execute correctly?

    After a couple of page loads, the debugger simply refuses to hit breakpoints. That's an annoying problem as well. But not my main beef, currently.

    I am trying to access a number of custom defined media types. My media types are 'RightSidebarSection', which contains an image, a link and a text.

    I have created a 'RightSidebarSections' folder in the media tab. I have given my home page the 'RightSidebarSections' property. I have added the folder to the property, just as the example above shows.

    At first, I hadn't imported linq. Now I have. It's still not working. Why can't I convert the Children property to a list?

    Also, why does the example insert a lambda statement right after the Children property on the line where it's looking for the Banner type? Does it not need to have a 'Where' call? Is the example even correct?

    Any help would be greatly appreciated. Thanks in advance.

  • Jay 49 posts 211 karma points
    Dec 23, 2014 @ 13:16
    Jay
    0

    I've now also tried to do it in a regular partial view, rather than a partial macro view. This also does not work for the same reason.

  • jivan thapa 194 posts 681 karma points
    Dec 23, 2014 @ 13:28
    jivan thapa
    100

    I assume that RightSidebarSections is property of the currentPage. and It returns Media folter ID.

     var rightSidebarSectionsId = 
            Model.Content.GetPropertyValue("RightSidebarSections").ToString();
    
        if ( !string.IsNullOrEmpty(rightSidebarSectionsId) 
            && Umbraco.TypedMedia(rightSidebarSectionsId) != null)
        {
            foreach (var m in Umbraco.TypedMedia(rightSidebarSectionsId).Children)
            {
                //  m.Id
            }
        }
    

    If RightSidebarSections is macro parameter, There is a different way to retrieve marco parameter value.

  • Jay 49 posts 211 karma points
    Dec 23, 2014 @ 14:17
    Jay
    0

    Thank you, that works well for me

    So how come this makes it work?

    var rightSidebarSectionsId = Model.Content.GetPropertyValue("RightSidebarSections", true).ToString();

    And this makes it not work?

    var rightSidebarSectionsId = CurrentPage.RightSideBarSections;

    Why does it work for the example I was referencing, but not for me? What have I done differently?

    Also, how come both these Where statements are valid?

    rightSidebarSectionsFolder.Children.Where(x => x.DocumentTypeAlias == "RightSidebarSection")
    rightSidebarSectionsFolder.Children.Where("Visible")

    Have you guys overloaded the Where method?

    Is the latter one simply shorthand? Does it check the 'Visible' property for true/false?

    Where might I find documentation on all these properties?

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Dec 23, 2014 @ 15:17
    Dennis Aaen
    0

    Hi Jay,

    I think this could exmplain it very well, I Umbraco there are two implementations of Razor the dynmaic version of Razor and the strongly typed version. You can read more about the dynamic version here http://our.umbraco.org/documentation/Reference/Templating/Mvc/views#Renderingafieldusing@CurrentPage%28dynamically%29, and about the strongly typed Razor here: http://our.umbraco.org/documentation/Reference/Mvc/partial-views#StronglytypedPartialViews

    And what you always had discovered you could create partial views or partial view macros. is @CurrentPage in the dynamic Razor and in the stongly typed version you use @Model.Content. To learn the difference between the dynamic razor and the strongly typed Razor I found these http://our.umbraco.org/projects/developer-tools/umbraco-v6-mvc-razor-cheatsheets cheatsheets very handy. Even though it say for MVC Umbraco version 6, you could also use this in Umbraco 7. The cheatsheets it just some PDF files that shows an overview of the Razor stuff, so it´s not a traditional package that you can install into your Umbraco installation.

    For the question where you find documentation on all these properties try to look here: http://our.umbraco.org/documentation/using-umbraco/backoffice-overview/property-editors/Built-in-Property-Editors-v7/ and sometimes here http://our.umbraco.org/documentation/using-umbraco/backoffice-overview/property-editors/Built-in-Property-Editors/ If you for instance using the tags data type you can find out the syntax for pulling out data here.http://our.umbraco.org/documentation/using-umbraco/backoffice-overview/property-editors/Built-in-Property-Editors/Tags As you can see there are a dynamic razor part and strongly typed razor part, And don't concentrate on the dynamic node razor it is an old razor implementation that is now deprecated, to use.

    Hope this helps,

    /Dennis

  • Jay 49 posts 211 karma points
    Dec 24, 2014 @ 10:26
    Jay
    0

    Dennis,

    Thank you for the clear explanation and the all the links. I will be digging through all this info in order to learn more.

    Will post again if I have more questions!

    Sincerely,

    Jay

  • Jay 49 posts 211 karma points
    Dec 24, 2014 @ 11:06
    Jay
    0

    How do you choose to work with either the dynamic/strong razor?

    I my partial view macros, I currently have the following bits of working code:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    <ul>
        @foreach (var page in CurrentPage.Children.Where("Visible").OrderBy("CreateDate desc"))
        { 
            <li><a href="@page.Url">@page.Name</a></li>
        }
    </ul>
    

    This code is dynamic, since it is using the CurrentPage property.

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @{
        var rightSidebarSectionsId = Model.Content.GetPropertyValue("RightSidebarSections", true).ToString();
    
        if (!string.IsNullOrEmpty(rightSidebarSectionsId) && Umbraco.TypedMedia(rightSidebarSectionsId) != null)
        {
            var rightSidebarSectionsFolder = Umbraco.TypedMedia(rightSidebarSectionsId);
            var rightSidebarSections = rightSidebarSectionsFolder.Children;
    
            foreach (var rightSidebarSection in rightSidebarSections)
            {
                <div class="divRightSidebarSection">
                    <a href="@rightSidebarSection.GetPropertyValue("Link", true)" target="_blank">
                        <img src="@rightSidebarSection.GetPropertyValue("UploadedImage", true)" style="width:240px;" />
                    </a>
                    @Html.Raw(@rightSidebarSection.GetPropertyValue("ImageText", true))
    
                </div>
            }
        }
    }
    

    This code is strong, because it's using Model.Content.

    I've learned that strongly typed razor views have a @model declaration at the top. I see no @model declaration at the top of my second bit of code. Yet it is strongly typed.

    How can this be? How will I know which one to use? Why does the example I linked to in my first post work dynamically, and why does my own code only work strongly?

  • Jay 49 posts 211 karma points
    Dec 24, 2014 @ 11:46
    Jay
    0

    Turns out that the dynamically accessed RightSidebarSections now also leads to correctly executing lambda expressions.

    Yesterday, my Visual Studio was constantly showing squiggly lines all over my Razor code, even the bits that were correctly executing.

    I have managed to fix this problem by running VS.NET with admin rights. Could it be that this very same problem was causing my code to crash on every Linq call?

    Oh well...

    The question about dynamic vs strong still stands, though. Is strong preferred over dynamic? Am I always free to choose which one to use? Or are there situations where you kinda have to use either dynamic or strong?

Please Sign in or register to post replies

Write your reply to:

Draft