Copied to clipboard

Flag this post as spam?

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


  • Martin 278 posts 662 karma points
    Jul 29, 2014 @ 15:17
    Martin
    0

    Can't Reference Individual Values - Razor Newbie

    Hi, I'm having trouble with pulling the individual values from the data grid. I can get the full string with both label and value, but i'm new to razor and having trouble.

    @widget.featureBlock gives me everything within the datatype.

    [ { "blockTitle": "Feature Block 1", "blockParagraph": "Feature Block 1 Content", "blockIcon": "1094", "urlPicker": "1062" }, { "blockTitle": "Feature Block 2", "blockParagraph": "feature Block 2 Content", "blockIcon": "1095", "urlPicker": "1054" } ]

    My razor macro is

    @{
        var widgetsFolder = Model.WidgetsFolders.Where("Visible").First();
    }
    
    @foreach (dynamic widget in widgetsFolder.Children.Where("Visible and nodeTypeAlias.StartsWith(\"Widget_\")")) {
    
        string widgetType = widget.NodeTypeAlias.ToString();
    
        switch (widgetType) {
            case "Widget_FeatureBlocksReveal":
            @showFeatureBlocksReveal(widget);
            break;
            default:
                <b>Unknown Widget!</b>
                break;
        }
    
    }
    
    @helper showFeatureBlocksReveal(dynamic widget){
    
    
        if (!string.IsNullOrEmpty(widget.featureBlock.ToString()))
        {
            foreach(dynamic content in widget.featureBlock)
            {
                @content.blockTitle
            }
        }   
    }
    

    Any help would be grateful.

    Martin

  • gilad 185 posts 425 karma points
    Jul 29, 2014 @ 16:01
    gilad
    0

    Hi Martin.

    Which version of umbraco do you use?

    Your helper looks fine..

    Try to use the example from the project page:

     

    @helper showFeatureBlocksReveal(dynamic widget){
       
    if(!string.IsNullOrEmpty(widget.featureBlock.ToString()))
       
    {
           
    foreach(var content in widget.featureBlock)
           
    {
                <p>
    @content.blockTitle</p>
           
    }
       
    }  
    }
  • Martin 278 posts 662 karma points
    Jul 29, 2014 @ 16:31
    Martin
    0

    Hi Gilad, thanks for your help.

    I tried to use the example razor from package page, but using CurrentPage didnt validate inside the macro and gave the following error.

    "error CS0103: The name 'CurrentPage' does not exist in the current context"

    @helper showFeatureBlocksReveal(dynamic widget){
        if(!string.IsNullOrEmpty(widget.featureBlock.ToString()))
        {
            foreach (var content in CurrentPage.widget.featureBlock)
            {
                <p>@content.blockTitle</p>
            }
        }   
    }
    

    Using the following helper, I am given an error of

    "Error loading MacroEngine script (file: RenderWidget.cshtml)"

    @helper showFeatureBlocksReveal(dynamic widget){
        if(!string.IsNullOrEmpty(widget.featureBlock.ToString()))
        {
            foreach (var content in widget.featureBlock)
            {
                <p>@content.blockTitle</p>
            }
        }   
    }
    

    When I remove the .blockTitle and leave just @content, the macro works, but outputs all the values of the grid data type.

    Again, any help would be appreciated. Martin

    Edit: Sorry im on the latest version of Umbraco installed through nuget

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies