Is it possible to check if a page has two properties, and if they both have values then *do something*? I have two dynamic lists for two separate content pickers, with a div wrapping them both. But I only want to show the div if at least one of the properties has a value.
At the moment can get it to show the div if one of the properties has a value, but can I check for two properties in the same if statement? My code so far:
I am pretty sure that HasValue will do what you need here
If you want to check that "at least one" has a value: @if(Model.HasValue("propertyOne") || Model.HasValue("propertyTwo"))
If you want to check both have a value: @if(Model.HasValue("propertyOne") && Model.HasValue("propertyTwo"))
If you want to check one but not the other has a value (XOR) @if(Model.HasValue("propertyOne") ^ Model.HasValue("propertyTwo"))
or, if you just want the value of propertyOne or propertyTwo you can use Coalesce. This is DynamicNull safe, so if the property doesn't exist on that node, it will fall back to the other case. Stack up as many properties as you want in Coalesce.
Thanks for the input guys, massive props! The Umbraco version I'm using is fairly out of date so I ended up going with this, it works great:
@{ var x = @Model.HasProperty("propertyOne") && @Model.GetProperty("propertyOne").Value != String.Empty; var y = @Model.HasProperty("propertyTwo") && @Model.GetProperty("propertyTwo").Value != String.Empty;
Check if page has two properties
Is it possible to check if a page has two properties, and if they both have values then *do something*? I have two dynamic lists for two separate content pickers, with a div wrapping them both. But I only want to show the div if at least one of the properties has a value.
At the moment can get it to show the div if one of the properties has a value, but can I check for two properties in the same if statement? My code so far:
My goal:
I realise this may not be the cleanest solution but you could do something like this:
I think this may work? There's probably a better solution though...
You can write a extension method for this purpose, something like:
}
I am pretty sure that HasValue will do what you need here
If you want to check that "at least one" has a value:
@if(Model.HasValue("propertyOne") || Model.HasValue("propertyTwo"))
If you want to check both have a value:
@if(Model.HasValue("propertyOne") && Model.HasValue("propertyTwo"))
If you want to check one but not the other has a value (XOR)
@if(Model.HasValue("propertyOne") ^ Model.HasValue("propertyTwo"))
or, if you just want the value of propertyOne or propertyTwo you can use Coalesce. This is DynamicNull safe, so if the property doesn't exist on that node, it will fall back to the other case. Stack up as many properties as you want in Coalesce.
@Library.Coalesce(Model.propertyOne, Model.propertyTwo)
Thanks for the input guys, massive props! The Umbraco version I'm using is fairly out of date so I ended up going with this, it works great:
@{
var x = @Model.HasProperty("propertyOne") && @Model.GetProperty("propertyOne").Value != String.Empty;
var y = @Model.HasProperty("propertyTwo") && @Model.GetProperty("propertyTwo").Value != String.Empty;
if((@x) || (@y)){do something}
else{do nothing}
}
is working on a reply...