If, for instance, it was a string, then you could just do:
var content = !String.IsNullOrEmpty(Model.GlobalContentPanel) ? Model.GlobalContentPanel : "<p>nothing to show at the moment</p>");
But if it's an IHtmlString (as returned by a rich-text property), then you'd check for a null:
var content = Model.GlobalContentPanel ?? new HtmlString("<p>nothing to show at the moment</p>");
Or you can check for empty (but not null) HTML by doing:
var content = !String.IsNullOrEmpty(Model.GlobalContentPanel.ToString()) ? Model.GlobalContentPanel : new HtmlString("<p>nothing to show at the moment</p>")
ModelsBuilder - Creating short conditions
I'm on Umbraco 7.7.2 with ModelsBuilder enabled.
I'm trying to learn how to check if if a property is not empty through the use of ModelsBuilder. This was my initial attempt...
If that looks correct, is there a way to shorten that into a single line like this...
At the moment, Visual Studio is complaining about this attempt.
Thanks for any insight
What type is
Model.GlobalContentPanel
?If, for instance, it was a string, then you could just do:
But if it's an
IHtmlString
(as returned by a rich-text property), then you'd check for a null:Or you can check for empty (but not null) HTML by doing:
If it's a different type, let us know...
ahh I didn't know any of this. My property is a RTE. and I simply want to check if its empty, and set my backup html text, if the value is empty.
I attempted to do the following (to test by backup text)...
...but with no content, it does not display
nothing to show at the moment
on my page. its just blank.I believe I found a working solution....
By default this will load my RTE content. But if empty, will display my backup HtmlString.
Thanks for helping me think this through!
is working on a reply...