How to see if a property of type Grid has content?
If I do call the HasValue method on a property whose type is a grid I always get true even if there is no actual content, but just the JSON with the definition of the empty grid.
Is there a way to detect whether a grid has content?
Assuming the alias of your grid property is content, your code could look like this:
@using Newtonsoft.Json.Linq
@inherits UmbracoTemplatePage
@{
Layout = "Master.cshtml";
if (CurrentPage.content is JObject) {
JObject grid = CurrentPage.content;
if (grid.SelectTokens("sections[*].rows[*].areas[*].controls").Any()) {
@CurrentPage.GetGridHtml("content", "fanoe")
}
}
}
Umbraco will expose the grid property value as a instance of JObject, which again has a SelectTokens method for matching one or more JSON tokens/objects - in this case to match the controls.
How to see if a property of type Grid has content?
If I do call the
HasValue
method on a property whose type is a grid I always gettrue
even if there is no actual content, but just the JSON with the definition of the empty grid.Is there a way to detect whether a grid has content?
Simone
Hi Simone,
Have you found any solution to that issue.
I'm currently having the same issue, and it's driving me nuts.
You could always fetch the value and compare it against the known structure of an empty grid.
Hi Simone,
Assuming the alias of your grid property is
content
, your code could look like this:Umbraco will expose the grid property value as a instance of
JObject
, which again has aSelectTokens
method for matching one or more JSON tokens/objects - in this case to match the controls.is working on a reply...