Copied to clipboard

Flag this post as spam?

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


  • Jacob Phillips 34 posts 84 karma points
    Jan 22, 2014 @ 01:48
    Jacob Phillips
    0

    Partial View Macro + Content Picker + Get Page Property

    Umbraco version 6.1.6

    Hi, I'm converting an XSLT macro to a razor partial view. I am accessing a content picker on a page in my razor partial view. I am able to get a list of Node ids from the content picker. All the node ids are of the same docType. The docType has a property called "teaser". I can't figure out how to get at each node ids teaser property. Note that I can get the individual node ids and page names. But not the custom properties. I've tried several things, but the macro just generates an generic error (my comments at the bottom.) Can anyone offer me any insights?

    Here's what I have:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @using Umbraco.Core.Dynamics;
    
    @* variables *@
    @{
        var targetPage = @Model.MacroParameters["targetPage"];
        var size = @Model.MacroParameters["size"];
        var numberToDisplay = @Model.MacroParameters["numberToDisplay"];
        var featureOn = @Model.MacroParameters["featureOn"];
        var featureTitle = @Model.MacroParameters["featureTitle"];
        var titleColor = @Model.MacroParameters["titleColor"];
    
    }   
    
    @{
        if(Model.Content.HasValue("cprHighlights"))
        {
            var typedMultiNodeTreePicker = new DynamicXml(Model.Content.GetPropertyValue<string>("cprHighlights"));
    
            var typedPublishedMNTPNodeList = new List<string>();
            foreach (dynamic id in typedMultiNodeTreePicker)                
            {
                typedPublishedMNTPNodeList.Add(id.InnerText); 
            }
            var typedMNTPCollection = Umbraco.TypedContent(typedPublishedMNTPNodeList).Where(x => x != null);
    
            var recordCount = typedMNTPCollection.Count();
    
    
            if(typedMNTPCollection.Any()) 
            {
                 <div class="homeHighlightsCarouselOutter">
                 @if(size == "Small")
                 {
    
                    <div class="head headerColorHome with-rss">
                        <h3>@featureTitle</h3>
                    </div> 
    
                 }
                else if(size == "Large") 
                {
                    <h3 class="homeHighlightsHeader headerColorHome">Highlights</h3>
                }
    
                @foreach (var item in typedMNTPCollection)
                {     
    
    
    
                               @* The following works *@
                    <p>@item.Name</p>         
                    <p>@item.Id</p> 
                               @* But when I try to do this: *@
                                <p>@item.teaser</p>
                               @* I just get an error *@
                     @* I have tried using the Id in item.Id to build a node *@
                var x = @Umbraco.Content(@item.Id);
                     @* and no error, but when I try to access it like this: *@
                                <p>@x.teaser</p>
                     @* I just get an error *@
    
                } 
                 </div> <!-- end class="homeHighlightsCarouselOutter --> 
            }
        }
    }
    
  • Alex Burr 77 posts 128 karma points
    Jan 22, 2014 @ 04:38
    Alex Burr
    0

    Does @item.Teaser work in your loop?

  • jacob phillips 130 posts 372 karma points
    Jan 22, 2014 @ 05:10
    jacob phillips
    0

    No, and I did try both @item.Teaser and @item.teaser

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Jan 22, 2014 @ 08:20
    Jeavon Leopold
    1

    Your issue is the additional @ within the code, blocks, e.g var x = @Umbraco.Content(@item.Id); should be var x = Umbraco.Content(item.Id); and var targetPage = @Model.MacroParameters["targetPage"]; should be var targetPage = Model.MacroParameters["targetPage"];

    Jeavon

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Jan 22, 2014 @ 08:37
    Dennis Aaen
    0

    Hi Jacob,

    I have just take a look at your code, and there are some things I think that can gives you the error. For the first I have removed the @ sign from where you creates the variables for the macro parameters. The next thing I think can give you the error is when you are written you comment to end the div.

    I think class and end are dedicated words in some programming lanagues maybe this can give some issues, and then I discovered that you have forgot to close the class in your comment. <!-- end class="homeHighlightsCarouselOutter -->

    So Jacob, could you try this one, hopefully it goes better.

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @using Umbraco.Core.Dynamics;

    @* variables *@
    @{
        var targetPage = Model.MacroParameters["targetPage"];
        var size = Model.MacroParameters["size"];
        var numberToDisplay = Model.MacroParameters["numberToDisplay"];
        var featureOn = Model.MacroParameters["featureOn"];
        var featureTitle = Model.MacroParameters["featureTitle"];
        var titleColor = Model.MacroParameters["titleColor"];

    }  

    @{
        if(Model.Content.HasValue("cprHighlights"))
        {
            var typedMultiNodeTreePicker = new DynamicXml(Model.Content.GetPropertyValue<string>("cprHighlights"));

            var typedPublishedMNTPNodeList = new List<string>();
            foreach (dynamic id in typedMultiNodeTreePicker)               
            {
                typedPublishedMNTPNodeList.Add(id.InnerText);
            }
            var typedMNTPCollection = Umbraco.TypedContent(typedPublishedMNTPNodeList).Where(x => x != null);

            var recordCount = typedMNTPCollection.Count();


            if(typedMNTPCollection.Any())
            {
                 <div class="homeHighlightsCarouselOutter">
                 @if(size == "Small")
                 {

                    <div class="head headerColorHome with-rss">
                        <h3>@featureTitle</h3>
                    </div>

                 }
                else if(size == "Large")
                {
                    <h3 class="homeHighlightsHeader headerColorHome">Highlights</h3>
                }

                @foreach (var item in typedMNTPCollection){    
                    <p>@item.Name</p>        
                    <p>@item.Id</p>
                    <p>@item.Teaser</p>
                 }
                </div>
            }
        }
    }

    Alternatively you could try to remove the end div comment, to check if you get any errors then I have done that in my post.

    Hope this can help you closer to get it working.

    /Dennis

  • Jacob Phillips 34 posts 84 karma points
    Jan 22, 2014 @ 19:14
    Jacob Phillips
    0

    Thx, Dennis and Jeavon.

    Dennis, I tried what you have posted exactly, and when I take out @item.Teaser, it works. If I have @item.Teaser or @item.teaser, it breaks. All I get on the front end is:

    Error loading Partial View script (file: ~/Views/MacroPartials/rCycleHighlightsRotator.cshtml)
    

    It seems I get an error if I try to access any custom property from the docType.

    Jeavon, Dennis' post follows your recommendation, except you have showed me this:

    var x = @Umbraco.Content(@item.Id); should be var x = Umbraco.Content(item.Id);
    

    Maybe I can get at the teaser property that way? Or is there some way I can see what it inside the item object? Or my var x object?

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Jan 22, 2014 @ 19:31
    Jeavon Leopold
    0

    Hi Jacob,

    You are using typed syntax not dynamic, so you need to do @item.GetPropertyValue("Teaser") not @item.Teaser (this would be dynamics)

    If you are only using the online Umbraco editor it maybe easier to use Dynamics, Typed is mainly used for intellisense in Visual Studio or WebMatrix.

    This would be your snippet in dynamic syntax (here @item.Teaser should would):

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @* variables *@
    @{
        var targetPage = Model.MacroParameters["targetPage"];
        var size = Model.MacroParameters["size"];
        var numberToDisplay = Model.MacroParameters["numberToDisplay"];
        var featureOn = Model.MacroParameters["featureOn"];
        var featureTitle = Model.MacroParameters["featureTitle"];
        var titleColor = Model.MacroParameters["titleColor"];
    
    }
    
    @{
        if (Model.Content.HasValue("cprHighlights"))
        {
            var dynamicPublishedMNTPNodeList = new List<string>();
            foreach (var id in CurrentPage.cprHighlights)
            {
                dynamicPublishedMNTPNodeList.Add(id.InnerText);
            }
            var dynamicMNTPCollection = Umbraco.Content(dynamicPublishedMNTPNodeList);
    
            var recordCount = dynamicMNTPCollection.Count()
    
            if (dynamicMNTPCollection.Any())
            {
                <div class="homeHighlightsCarouselOutter">
                    @if (size == "Small")
                    {
    
                        <div class="head headerColorHome with-rss">
                            <h3>@featureTitle</h3>
                        </div>
    
                    }
                    else if (size == "Large")
                    {
                        <h3 class="homeHighlightsHeader headerColorHome">Highlights</h3>
                    }
    
                    @foreach (var item in dynamicMNTPCollection)
                    {
                        <p>@item.Name</p>
                        <p>@item.Id</p>
                        <p>@item.Teaser</p>
                    }
                </div>
            }
        }
    }
    

    Jeavon

  • Jacob Phillips 34 posts 84 karma points
    Jan 22, 2014 @ 20:04
    Jacob Phillips
    0

    Thank you for pointing out dynamic vs. typed syntax and their usage.

    The example you give generates error, but I think it's just because of how my nodes in content picker are stored?

    If I reduce snippet just down to this:

    if (Model.Content.HasValue("cprHighlights"))
    {
        var dynamicPublishedMNTPNodeList = new List<string>();
        <p>@CurrentPage.cprHighlights</p>
    
    }
    

    I can see my node values returned like this:

    <MultiNodePicker type="content"><nodeId>14122</nodeId><nodeId>14007</nodeId><nodeId>13876</nodeId><nodeId>13926</nodeId><nodeId>13939</nodeId></MultiNodePicker>
    

    But when I loop through it as here:

    if (Model.Content.HasValue("cprHighlights"))
    {
        var dynamicPublishedMNTPNodeList = new List<string>();
        foreach(var id in CurrentPage.cprHighlights)
        {
            dynamicPublishedMNTPNodeList.Add(id.InnerText);
        }
    
    }
    

    I get

    Error loading Partial View script
    
  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Jan 22, 2014 @ 20:21
    Jeavon Leopold
    0

    Ah ok, can you confirm your MNTP settings, I'm guessing you are storing as CSV rather than XML?

    Try this instead:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @* variables *@
    @{
        var targetPage = Model.MacroParameters["targetPage"];
        var size = Model.MacroParameters["size"];
        var numberToDisplay = Model.MacroParameters["numberToDisplay"];
        var featureOn = Model.MacroParameters["featureOn"];
        var featureTitle = Model.MacroParameters["featureTitle"];
        var titleColor = Model.MacroParameters["titleColor"];
    
    }
    @{
        if (Model.Content.HasValue("cprHighlights"))
        {
            var dynamicPublishedMNTPNodeList = CurrentPage.cprHighlights.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            var dynamicMNTPCollection = Umbraco.Content(dynamicPublishedMNTPNodeList);
    
            var recordCount = dynamicMNTPCollection.Count();
    
            if (dynamicMNTPCollection.Any())
            {
                <div class="homeHighlightsCarouselOutter">
                    @if (size == "Small")
                    {
    
                        <div class="head headerColorHome with-rss">
                            <h3>@featureTitle</h3>
                        </div>
    
                    }
                    else if (size == "Large")
                    {
                        <h3 class="homeHighlightsHeader headerColorHome">Highlights</h3>
                    }
    
                    @foreach (var item in dynamicMNTPCollection)
                    {
                        <p>@item.Name</p>
                        <p>@item.Id</p>
                        <p>@item.Teaser</p>
                    }
                </div>
            }
        }
    }
    
  • Jacob Phillips 34 posts 84 karma points
    Jan 22, 2014 @ 20:41
    Jacob Phillips
    0

    My MNTP data type has "XML" checked, and not "CSV".

    This one is for "CSV" type, yeah?:

    var dynamicPublishedMNTPNodeList = CurrentPage.cprHighlights.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
    
  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Jan 22, 2014 @ 21:00
    Jeavon Leopold
    100

    Yes, that's right.

    I've just fully tested the below snippet with XML storage and it's working, could you try?

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @* variables *@
    @{
        var targetPage = Model.MacroParameters["targetPage"];
        var size = Model.MacroParameters["size"];
        var numberToDisplay = Model.MacroParameters["numberToDisplay"];
        var featureOn = Model.MacroParameters["featureOn"];
        var featureTitle = Model.MacroParameters["featureTitle"];
        var titleColor = Model.MacroParameters["titleColor"];
    
    }
    @{
        if (CurrentPage.HasValue("cprHighlights"))
        {       
            var dynamicPublishedMNTPNodeList = new List<string>();
            foreach (var id in CurrentPage.cprHighlights)
            {
                dynamicPublishedMNTPNodeList.Add(id.InnerText);
            }
            var dynamicMNTPCollection = Umbraco.Content(dynamicPublishedMNTPNodeList);
    
            var recordCount = dynamicMNTPCollection.Count();
    
            if (dynamicMNTPCollection.Any())
            {            
                <div class="homeHighlightsCarouselOutter">
    
                    @if (size == "Small")
                    {
                        <div class="head headerColorHome with-rss">
                            <h3>@featureTitle</h3>
                        </div>
    
                    }
                    else if (size == "Large")
                    {
                        <h3 class="homeHighlightsHeader headerColorHome">Highlights</h3>
                    }
    
                    @foreach (var item in dynamicMNTPCollection)
                    {
                        <p>@item.Name</p>
                        <p>@item.Id</p>
                        <p>@item.Teaser</p>
                    }
                </div>
            }
        }
    }
    
  • Jacob Phillips 34 posts 84 karma points
    Jan 22, 2014 @ 21:01
    Jacob Phillips
    0

    I was thinking that in my earlier example, this is right for getting the node ids out of my MNTP:

    var typedMultiNodeTreePicker = new DynamicXml(Model.Content.GetPropertyValue<string>("cprHighlights"));
    

    But when I try to use it like this:

        @{
        if(Model.Content.HasValue("cprHighlights"))
        {
            var dynamicPublishedMNTPNodeList = new List<string>();
            var typedMultiNodeTreePicker = new DynamicXml(Model.Content.GetPropertyValue<string>("cprHighlights"));
            foreach (dynamic id in typedMultiNodeTreePicker)                
            {
                dynamicPublishedMNTPNodeList.Add(id.InnerText); 
            }
            var dynamicMNTPCollection = Umbraco.Content(dynamicPublishedMNTPNodeList);
    
                    foreach (var item in dynamicMNTPCollection)
                    {
                        <p>@item.Name</p>
                        <p>@item.Id</p>
    
                    }
       }
    }
    

    I get error.

    Is there a different method for typed vs. dynamic collection?

    After foreach loop I used to have this:

    var typedMNTPCollection = Umbraco.TypedContent(typedPublishedMNTPNodeList).Where(x => x != null);
    

    But we replaced with this:

    var dynamicMNTPCollection = Umbraco.Content(dynamicPublishedMNTPNodeList);
    

    So, if I put it back together with DynamicXml method, and strip out some of the extra stuff for simplicity here, I got this and it works:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @using Umbraco.Core.Dynamics;
    
    @* variables *@
    @{
        var targetPage = Model.MacroParameters["targetPage"];
        var size = Model.MacroParameters["size"];
        var numberToDisplay = Model.MacroParameters["numberToDisplay"];
        var featureOn = Model.MacroParameters["featureOn"];
        var featureTitle = Model.MacroParameters["featureTitle"];
        var titleColor = Model.MacroParameters["titleColor"];
    
    }
    
    @{
        if(Model.Content.HasValue("cprHighlights"))
        {
            var dynamicPublishedMNTPNodeList = new List<string>();
            var typedMultiNodeTreePicker = new DynamicXml(Model.Content.GetPropertyValue<string>("cprHighlights"));
            foreach (dynamic id in typedMultiNodeTreePicker)                
            {
                dynamicPublishedMNTPNodeList.Add(id.InnerText); 
            }
    
            var dynamicMNTPCollection = Umbraco.Content(dynamicPublishedMNTPNodeList);
    
            foreach (var item in dynamicMNTPCollection)
            {
                <p>@item.Name</p>
                <p>@item.Id</p>
                <p>@item.Teaser</p>
            }
        }
    }
    

    Is it organizationationally sound? Or am I mixing Typed and Dynamic syntax again? I think I am

  • Jacob Phillips 34 posts 84 karma points
    Jan 22, 2014 @ 21:07
    Jacob Phillips
    0

    Regarding your last post Jeavon; I tried it and yeah, it works. Yours also doesn't mix the dynamic syntax I think. I thought we tried that!?

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Jan 22, 2014 @ 21:19
    Jeavon Leopold
    0

    That's great! We did try it except for the HasValue bit, but that shouldn't cause an error.....? That final snippet is all dynamic syntax.

    You can mix and match dynamic and typed just as long as you understand what you are doing where.

  • Jacob Phillips 34 posts 84 karma points
    Jan 22, 2014 @ 21:24
    Jacob Phillips
    0

    Ok, I diff'd. I see it.

    if (Model.Content.HasValue("cprHighlights"))
    

    vs.

    if (CurrentPage.HasValue("cprHighlights"))
    

    .....

    Thanks for taking the time and explanations, I'm moving fwd again. You are the man.

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Jan 22, 2014 @ 21:30
    Jeavon Leopold
    0

    You're welcome, if you change back to if (Model.Content.HasValue("cprHighlights")) does it break?

    p.s. Please mark solution on correct snippet :-)

  • Jacob Phillips 34 posts 84 karma points
    Jan 22, 2014 @ 21:32
    Jacob Phillips
    0

    No it does not break.

    I see in the earlier post semi-colon missing on this one:

    var recordCount = dynamicMNTPCollection.Count();
    

    Must've been it.

  • Jacob Phillips 34 posts 84 karma points
    Jan 22, 2014 @ 21:36
    Jacob Phillips
    0

    I don't see any option to mark solution. So I just high-fived the correct snippet.

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Jan 22, 2014 @ 21:40
    Jeavon Leopold
    0

    Ah phew, all good then.

    Do you have a green tick under the name of a author on each comment? That is the mark solution button so should be clicked only on the correct solution.

  • Jacob Phillips 34 posts 84 karma points
    Jan 22, 2014 @ 21:44
    Jacob Phillips
    0

    I see it and checked it. Thx.

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Jan 22, 2014 @ 22:13
    Jeavon Leopold
    0

    Awesome!

Please Sign in or register to post replies

Write your reply to:

Draft