Copied to clipboard

Flag this post as spam?

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


  • wschwarte 44 posts 73 karma points
    Sep 04, 2014 @ 10:50
    wschwarte
    0

    Need help with visual studio and mvc razor and getting a property value

    I am struggling with this MVC/Razor thing again. So I installad a site using WebMatrix and also installed VS Web 2013. In WebMAtrix I press the VS button to start my coding, hoping for some intellisene etc. 

    I want to loop through a node that contains children. Each child is an item for in a carrousel slider. So now I created a MVC Partial Macro for this.

    In the foreach i want to check if the current child (page) has a value for sliderLink (contentPicker).

    1. I cannot get intellisense (see picture). Is this normal? Or do i need to change something. 

    2. How can i check if a page was picked. Model.Content.HasValue does only contain properties of the currentpage (if i'm correct).  

    hope somebody can help me with this

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Sep 04, 2014 @ 11:08
    Jeavon Leopold
    0

    Switch to Umbraco.TypedContent and then it will work. Also you should use Umbraco.TypedMedia, Umbraco.TypedMember etc

  • crono 58 posts 129 karma points
    Sep 04, 2014 @ 11:11
    crono
    0

    Dynamic objects are just that - dynamic. Meaning that they can be just about anything you want. That is why you don't get any intellisense (what is it supposed to 'sense' when it can be anything?).

    Instead of 'Umbraco.Content(id)' use 'Umbraco.TypedContent(id)' - that way, intellisense will know what object type you are using.

    Hope that answers your question :)

  • wschwarte 44 posts 73 karma points
    Sep 04, 2014 @ 11:13
    wschwarte
    0

    Where do i do this switch?

  • crono 58 posts 129 karma points
    Sep 04, 2014 @ 11:15
    crono
    0

    Change

    var startNode = Umbraco.Content(startNodeID);
    

    Into

    var startNode = Umbraco.TypedContent(startNodeID);
    

    That will give you full intellisense on that object.

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Sep 04, 2014 @ 11:19
    Jeavon Leopold
    0

    You have var startNode = Umbraco.Content(startNodeID); change to var startNode = Umbraco.TypedContent(startNodeID);

  • wschwarte 44 posts 73 karma points
    Sep 04, 2014 @ 11:35
    wschwarte
    0

    Thanks. 

    So, i used to have @page.sliderCaption (which is a textbox custom property). But now i have changed accordingly, @page.sliderCaption does not work anymore and I have to use page.GetPropertyValue("sliderCaption"). Why is that?

     

     

     

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Sep 04, 2014 @ 11:39
    Jeavon Leopold
    0

    That is dynamics vs strongly typed.

    Dynamics is concise in that you can do CurrentPage.propertyName but Strongly Typed gives you intellisense but you cannot use dynamic property names.

    Something you could consider is the Zbu.ModelsBuilder package which generates strongly typed models from your document types meaning you can do Model.Content.PropertyName and have Intellisense!

    There is a video demo of using it here

  • wschwarte 44 posts 73 karma points
    Sep 04, 2014 @ 11:59
    wschwarte
    0

    Getting there :-)

    So would this be a correct way of coding this, or am i taking some unneeded detours?

       @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    
    @*
        === Macro Parameters To Create ===
        Alias:nodeId Name:Node ID    Type:Content Picker
    *@
    
    
    @{
    
        var startNodeID = Model.MacroParameters["carrouselNodeId"];
    }
    
    
    @if (startNodeID != null)
    {
        @* Get the start node as a dynamic node *@
        var startNode = Umbraco.TypedContent( startNodeID);
    
        if (startNode.Children.Where("Visible").Any())
        {
    
    <!-- Slider -->
        <div id="slider-block">
        <div id="slider-holder">
        <div id="slider">
    
    
                @foreach (var page in startNode.Children.Where("Visible"))
                {
                    var sliderImageUrl = string.Empty;
                    var caption = string.Empty;
                    var linkedPageUrl = string.Empty;
    
                    // get caption
                    caption = (string)page.GetPropertyValue("sliderCaption");
    
                    // get image url
                    if (page.HasValue("sliderImage"))
                    {
                        sliderImageUrl = page.GetCropUrl("sliderImage", "Carrousel Afbeelding");
                    }
    
                    if (page.HasValue("sliderLink"))
                    {
                        var pageToLink = Umbraco.TypedContent(page.GetPropertyValue("sliderLink"));
                        linkedPageUrl = @pageToLink.Url;
    
                        <a href="@linkedPageUrl">
                            <img src="@sliderImageUrl" title="@caption" alt="@caption" />
                        </a>
                    }
                    else
                    {
                        <img src="@sliderImageUrl" title="@caption" alt="@caption" />
                    }
    
                }
    
    
        </div>
        </div>
        </div>
    <!-- ENDS Slider -->    
    
    
              }
    
    }
    
  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Sep 04, 2014 @ 16:52
    Jeavon Leopold
    0

    Generally pretty good, I have made a few minor tweaks below:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @{
        var startNodeId = Model.MacroParameters["carrouselNodeId"];
    
        if (startNodeId != null)
        {
            @* Get the start node as a dynamic node *@
            var startNode = Umbraco.TypedContent(startNodeId);
    
            if (startNode.Children.Where("Visible").Any())
            {
    
                <!-- Slider -->
                <div id="slider-block">
                    <div id="slider-holder">
                        <div id="slider">
    
    
                            @foreach (var page in startNode.Children.Where("Visible"))
                            {
                                var sliderImageUrl = string.Empty;
    
                                // get caption
                                var caption = page.GetPropertyValue<string>("sliderCaption");
    
                                // get image url
                                if (page.HasValue("sliderImage"))
                                {
                                    sliderImageUrl = page.GetCropUrl("sliderImage", "Carrousel Afbeelding");
                                }
    
                                if (page.HasValue("sliderLink"))
                                {
                                    var pageToLink = Umbraco.TypedContent(page.GetPropertyValue("sliderLink"));
                                    var linkedPageUrl = pageToLink.Url;
    
                                    <a href="@linkedPageUrl">
                                        <img src="@sliderImageUrl" title="@caption" alt="@caption" />
                                    </a>
                                }
                                else
                                {
                                    <img src="@sliderImageUrl" title="@caption" alt="@caption" />
                                }
    
                            }
    
                        </div>
                    </div>
                </div>
                <!-- ENDS Slider -->
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft