When using the CMS, we need to display the values on the front-end, in doing so we need to check that the editor has entered values into the properties.
In this blog, I will show you how to check for null values in an Umbraco RTE, string and image media.
If the property has a value, it will be displayed, if the property does not have a value, we display the default value.
The code for this example can be downloaded from GitHub with the link at the bottom of the page.
To set this example up, I have created a view model, controller and new doctype in the CMS. With these in place, I can now display the examples.
View Model Code
As you can see in the code below, for the text string I have set a default value of 'I have no value', for the RTE the default value is 'RTE has no value' and for the image, you can see I'm first checking for a string, if no string exists, the default value is 'NA', the reason for this is Umbraco media returns an int for the image, so I do a check to see if the value is an int, if it is, I can then get the URL for the image.
So now that all the checks are in place, I can now check if property has value or check for null.
If you now click info in the CMS and click the link 'check-for-nulls' you can now see the results.
That wraps it up for Umbraco check if property has value, if you found it helpful, please leave a comment below.