Copied to clipboard

Flag this post as spam?

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


  • Nick 84 posts 451 karma points
    Feb 25, 2015 @ 11:13
    Nick
    0

    Umbraco.TypedMedia

    Morning All,

    I have having trouble creating a simple slideshow within a Partial Macro View, I am using the latest Umbraco version of 7.2. My script for the carousel is below, using the Content picker to choose the start node, and to display the child nodes within a carousel slideshow, I can't seem to get this to work any help much appreciated.

     

    Kind Regards

     

    Nick

     

    @inherits Umbraco.Web.Macros.PartialViewMacroPage

    @*

        === Macro Parameters To Create ===

        Show:True   Alias:nodeId Name:Node ID    Type:Content Picker

    *@

     

    @if (Model.MacroParameters["startNodeID"] != null)

    {

         @* Get the start node as a dynamic node *@

         var startNode = Umbraco.Content(Model.MacroParameters["startNodeID"]);

     

         if (startNode.Children.Where("Visible").Any())

        {

    <h2>Our clients</h2>

           <div id="client-list"> 

                @foreach (var page in startNode.Children.Where("Visible"))

      {

    if(Model.Content.HasValue("clientLogo")){

    var mediaItem = Umbraco.TypedMedia(Model.Content.GetPropertyValue("clientLogo")); 

    <div class="item"><img src="@mediaItem.GetPropertyValue("umbracoFile")"/></div>

    }    

    }

            </div>

        }    

    }

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Feb 25, 2015 @ 11:23
    Jan Skovgaard
    0

    Hi Nick

    What does your current node render? Do you get the image path? Or what happens?

    And have you some JavaScript in place for making the caroussel as well?

    /Jan

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Feb 25, 2015 @ 11:23
    Dennis Aaen
    0

    Hi Nick,

    What if you change this snippet of your code

    if(Model.Content.HasValue("clientLogo")){

       var mediaItem = Umbraco.TypedMedia(Model.Content.GetPropertyValue("clientLogo"));

       <div class="item"><img src="@mediaItem.GetPropertyValue("umbracoFile")" /></div>

    }   

    To this snippet.

    if(page.Content.HasValue("clientLogo")){

       var mediaItem = Umbraco.TypedMedia(page.Content.GetPropertyValue("clientLogo"));

      <div class="item"><img src="@mediaItem.GetPropertyValue("umbracoFile")" /></div>

    }   

    Hope this helps,

    /Dennis

  • Nick 84 posts 451 karma points
    Feb 25, 2015 @ 12:39
    Nick
    0

    Many thanks for the quick replies:

    @Jan Yes JS is in place for the carousel

    @Dennis: Thnaks for the amended code, but now receive a different error

    Error loading partial view macro (View: ~/Views/MacroPartials/Slideshow.cshtml). Exception: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary,    and refernces line 8 as the error - @if (Model.MacroParameters["startNodeID"] != null)

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Feb 25, 2015 @ 12:42
    Jan Skovgaard
    0

    Hi Nick

    Ok, was just not sure about what the initial issue was.

    In regards to testing the parameter then you should try this

    @if(!String.IsNullOrEmpty(@Model.MacroParameters["startNodeID"].ToString())){
      //Your code here
    }
    

    Hope this helps.

    /Jan

  • Nick 84 posts 451 karma points
    Feb 25, 2015 @ 13:50
    Nick
    0

    Thanks Jan;

    This is my code now:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage

     

    @*

        === Macro Parameters To Create ===

        Show:True   Alias:nodeId Name:Node ID    Type:Content Picker

    *@

     

    @if(!String.IsNullOrEmpty(@Model.MacroParameters["startNodeID"].ToString())){

    @* Get the start node as a dynamic node *@

         var startNode = Umbraco.Content(Model.MacroParameters["startNodeID"]);

         if (startNode.Children.Where("Visible").Any())

        {

    <h2>Our clients</h2>

           <div id="client-list"> 

                @foreach (var page in startNode.Children.Where("Visible"))

      {

    if(page.Content.HasValue("clientLogo")){

      var mediaItem = Umbraco.TypedMedia(page.Content.GetPropertyValue("clientLogo")); 

      <div class="item"><img src="@mediaItem.GetPropertyValue("umbracoFile")" /></div>

    }

      }  

           </div>

        }  

     

    But still recive the same error:

    Error loading partial view macro (View: ~/Views/MacroPartials/Slideshow.cshtml). Exception: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.

       at System.Collections.Generic.Dictionary`2.get_Item(TKey key)

       at ASP._Page_Views_MacroPartials_Slideshow_cshtml.Execute()  on line 8

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Feb 25, 2015 @ 14:46
    Dennis Aaen
    101

    Hi Nick,

    With this code you should be good. I think the problem here is that you mixing the dynamic razor and strongly typed razor. I have change it so all of it is in dynamic razor. And I was able to get images outputted.

    @inherits Umbraco.Web.Macros.PartialViewMacroPage

    @*
        === Macro Parameters To Create ===

        Show:True   Alias:nodeId Name:Node ID    Type:Content Picker

    *@


    @if(!String.IsNullOrEmpty(Model.MacroParameters["startNodeID"].ToString())){
       
        var startNode = Umbraco.Content(Model.MacroParameters["startNodeID"]);
       
        if (startNode.Children.Where("Visible").Any()){
           
            <h2>Our clients</h2>
           
            <div id="client-list">
                @foreach (var page in startNode.Children.Where("Visible")){
                    @page.Name
                   
                    if(page.HasValue("clientLogo")){
                        var mediaItem = Umbraco.Media(page.clientLogo);
                       
                        <div class="item"><img src="@mediaItem.umbracoFile" /></div>
                    }
                }
           
            </div>
        }

    I have made it in the dynamics version of Razor. In Umbraco you have to different razor syntaxs, one is the dynamics CurrentPage, and the other is strongly typed Model.Content. The different is that the dynamics implemtation is more concise and with the strongly typed razor you will get intellisense if you are using e.g visual studio or webmatrix.

    You could try to see Jeavon´s slides from the umbOktoberFest 2014 which you can download here. I so think it helps if you see this https://our.umbraco.org/projects/developer-tools/umbraco-v6-mvc-razor-cheatsheets these are pdf cheat sheets about Razor. I know it´s says for Umbraco 6, but you can also use the overview for Umbraco 7.

    Hope this helps, if you have other questions about this don't hesitate to asking again.

    /Dennis

  • Nick 84 posts 451 karma points
    Feb 25, 2015 @ 14:52
    Nick
    0

    Many thanks Dennis, this works great, still getting to grips with Razor.

    Thanks again for your patience and help.

     

    Nick

Please Sign in or register to post replies

Write your reply to:

Draft