Copied to clipboard

Flag this post as spam?

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


  • Elin 45 posts 166 karma points
    Jul 14, 2017 @ 13:13
    Elin
    0

    How to check if Umbraco Grid has values

    Hi,

    Is there an easy way to verify if the grid in umbraco has value ?

    Basically what I'm trying to do is to check if has any content in it, something similar to the below code but for the grid.

    @if (CurrentPage.HasValue("field-name")
    

    I tried using the grid's field name, but even when the grid is empty, it still come back as if the grid had some invisible values.

    I don't know if it will help but below is a piece of the code that I'm working with; any help will be greatly appreciated.

    @if (CurrentPage.HasValue("section6Title") || CurrentPage.HasValue("section6Subtitle") || CurrentPage.HasValue("section6MainContent")) {
        <div class="stellar-section">
            @Umbraco.RenderMacro("IconListBanner")
        </div><!-- .stellar-section -->
    }
    
  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jul 14, 2017 @ 18:35
    Dennis Aaen
    0

    Hi Elin,

    I found a similar thread at the forum. Perhaps you can use the approach to check if the grid has a value.

    https://our.umbraco.org/forum/umbraco-7/using-umbraco-7/73346-check-if-umbraco-grid-is-empty

    Hope this helps,

    /Dennis

  • Elin 45 posts 166 karma points
    Jul 14, 2017 @ 18:42
    Elin
    0

    Hi Dennis,

    Thanks for replying.

    I went through that thread already, and also commented (Last post at the bottom of the thread).

    In brief I need a solution that doesn't involve downloading a package, as things might get too complicated.

    I need something similar to the "HasValue" in order to check if the grid is empty or actually has contents.

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jul 14, 2017 @ 18:46
    Dennis Aaen
    0

    Hi Elin,

    Just so we know what exact version of Umbraco are you using?

    Best,

    /Dennis

  • Elin 45 posts 166 karma points
    Jul 14, 2017 @ 18:48
    Elin
    0

    I'm using Umbraco Cloud, the latest version as far as I know (as it gets updated automatically).

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jul 14, 2017 @ 18:50
    Dennis Aaen
    0

    HI Elin,

    Okay I will try to see if I can find a way to check this with out any further packages installed.

    I will come back to you with the results I find.

    Best,

    -Dennis

  • Elin 45 posts 166 karma points
    Jul 14, 2017 @ 18:52
    Elin
    0

    Thanks a lot for your help, I really appreciate it.

  • Anders Bjerner 487 posts 2989 karma points MVP 8x admin c-trib
    Jul 14, 2017 @ 19:03
    Anders Bjerner
    1

    I can really recommend using my GridData package. If you're using v2.0 or newer, you can do something like this:

    @using Skybrud.Umbraco.GridData
    @using Skybrud.Umbraco.GridData.Extensions
    @inherits UmbracoTemplatePage
    @{
        Layout = "Master.cshtml";
    
        GridDataModel grid = Model.Content.GetGridModel("content");
    
        if (grid.IsValid) {
            @grid.GetHtml(Html, "fanoe")
        }
    
    }
    

    Anyways, if you haven't installed my package, you can do something like this instead:

    @using Newtonsoft.Json.Linq
    @inherits UmbracoTemplatePage
    @{
        Layout = "Master.cshtml";
    
        JObject grid = CurrentPage.content;
    
        if (grid.SelectTokens("sections[*].rows[*].areas[*].controls").Any()) {
            @CurrentPage.GetGridHtml("content", "fanoe")
        }
    
    }
    

    Umbraco uses an instance of JObject to represent the grid value, and as such we can use the SelectTokens method along with a JPath (similar to XPath, just for JSON) to match the grid controls.

  • Elin 45 posts 166 karma points
    Jul 14, 2017 @ 20:11
    Elin
    0

    Hi Anders,

    I tried your suggestion but I get an error code, see below.

    Cannot implicitly convert type 'Umbraco.Core.Dynamics.DynamicNull' to 'Newtonsoft.Json.Linq.JObject

    I tried replacing the JObject in front of the grid with a var but that didn't do anything.

    I pasted my entire code section below maybe I'm doing something wrong that I can't figure out yet

        @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
        @inherits System.Web.Mvc.WebViewPage
        @using Newtonsoft.Json.Linq
        @inherits UmbracoTemplatePage
        @{
            Layout = "Template.cshtml";
    
            JObject grid = CurrentPage.content;
    
            if (grid.SelectTokens("sections[*].rows[*].areas[*].controls").Any()) {
                @CurrentPage.GetGridHtml("section1Grid", "Bootstrap3-Fluid")
            }
        }
        @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
        @using umbraco.cms.businesslogic.web;
        @section HeadContent {
            <link rel="stylesheet" href="/css/dynamic-template.css">
        }
    
        @section BodyContent {
            <!--content-->
            <div id="standard-page-banner" class="stellar-section">
                @Umbraco.RenderMacro("StandardPageBanner")
            </div><!-- #standard-page-banner .stellar-section -->
    
            @if (CurrentPage.HasValue("section1Title") || CurrentPage.HasValue("section1Subtitle") || CurrentPage.HasValue("section1MainContent")) {
                <div id="section-1" class="thumb-box4 light-background">
                    <div class="container">
                        <p class="title wow fadeInDown animated"><strong>@Umbraco.Field("section1Title", casing: RenderFieldCaseType.Upper, convertLineBreaks:true)</strong></p>
                        <p class="sub-title wow fadeInLeft animated">@Umbraco.Field("section1Subtitle", casing: RenderFieldCaseType.Upper, convertLineBreaks:true)</p>
                        <p class="text wow fadeInLeft animated">@Umbraco.Field("section1MainContent", convertLineBreaks: true, removeParagraphTags: true)</p>
                        @CurrentPage.GetGridHtml(Html, "section1Grid", "Bootstrap3-Fluid")
                    </div><!-- .container .wow .fadeInRight -->
                </div><!-- #section-1 .thumb-box4 .light-background -->
            }
    }
    

    I have to say that I've only been using Umbraco for about a year, so I'm still fairly new and there's still a lot for me to learn.

    Thanks

  • Anders Bjerner 487 posts 2989 karma points MVP 8x admin c-trib
    Jul 14, 2017 @ 21:08
    Anders Bjerner
    100

    Hi again,

    In my examples, content was the alias of my property, so your code fails because you don't have a property with that alias.

    From the code you've posted, it seems that your property alias is section1Grid, so the line that fails should be this instead:

     JObject grid = CurrentPage.section1Grid;
    

    To be extra sure, you can also add another validation check:

    if (CurrentPage.section1Grid is JObject) {
    
        JObject grid = CurrentPage.section1Grid;
    
        // the rest of your code
    
    }
    

    This will check whether the value of the property is fact of the type JObject.

Please Sign in or register to post replies

Write your reply to:

Draft