Null check blockgrid
# help-with-umbraco
u
Im using @await Html.GetBlockGridItemAreasHtmlAsync(Model) to render some nested blockgrid. How do i null check this to see if the current block has child blocks? It works fine as it is in the frontend, but i get an error in the backoffice custom view when inserting a block that i empty (and therefor unable to add children inside it).
r
If I remember right,
Model
in this context is an
Enumerable
type under the hood so you can do something like
if (Model != null)
or
if (Model.Any())
u
Well Hello mr. Butterfield 🙂 im actually using your package for backoffice previews
@if (Model.Any()) { .... } returns: "Something went wrong rendering a preview. Exception of type 'Umbraco.Cms.Web.Common.ModelsBuilder.InMemoryAuto.UmbracoCompilationException' was thrown." @if (Model != null){ ... } returns: "Something went wrong rendering a preview. Object reference not set to an instance of an object."
r
There may be a property on Model then! Such as
Model.Areas
or
Model.Blocks
u
Strange... I left it over the weekend, not working when i left it. But now @if (Model != null){ works... I guess a rebuild was in order 🙂
a
My favorite is @if(Model?.Any() == true){ ... }
a
Rather make a extension method like
Model.NotNullAndHasAny()
or something 😛
m
in Areas.chtml.. I have
if (Model?.Areas.Any() != true) { return; }
for an early exit.. Which I think, if memory serves, also covers Areas without a Block too? 🤔 Or
if (Model?.Areas.Any(x=>x.Any()) != true) { return; }
?? (for if you allow layouts in areas too)? To get an actual block in an area had to use..
var x = item.Areas.FirstOrDefault(x => x.Any(y => y.Content.ContentType.Alias == GridShowMore.ModelTypeAlias));