Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
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 --> }
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
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.
Just so we know what exact version of Umbraco are you using?
Best,
I'm using Umbraco Cloud, the latest version as far as I know (as it gets updated automatically).
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.
-Dennis
Thanks a lot for your help, I really appreciate it.
I can really recommend using my GridData package. If you're using v2.0 or newer, you can do something like this:
v2.0
@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.
JObject
SelectTokens
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.
var
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
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.
content
From the code you've posted, it seems that your property alias is section1Grid, so the line that fails should be this instead:
section1Grid
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.
is working on a reply...
Write your reply to:
Upload image
Image will be uploaded when post is submitted
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.
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.
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
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.
Hi Elin,
Just so we know what exact version of Umbraco are you using?
Best,
/Dennis
I'm using Umbraco Cloud, the latest version as far as I know (as it gets updated automatically).
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
Thanks a lot for your help, I really appreciate it.
I can really recommend using my GridData package. If you're using
v2.0
or newer, you can do something like this:Anyways, if you haven't installed my package, you can do something like this instead:
Umbraco uses an instance of
JObject
to represent the grid value, and as such we can use theSelectTokens
method along with a JPath (similar to XPath, just for JSON) to match the grid controls.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 avar
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
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
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:To be extra sure, you can also add another validation check:
This will check whether the value of the property is fact of the type
JObject
.is working on a reply...