Umbraco check if property has value

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

View Model
using System.Web;

namespace Web.Model.NullCheck
{
    public class CheckForNullViewModel
    {
        public string TestString        { get; set; }
        public string TestImage         { get; set; }
        public HtmlString TestRte       { get; set; }
    }
}

Controller 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.

Controller Code
using System.Web;
using System.Web.Mvc;
using Umbraco.Core.Models;
using Umbraco.Web;
using Umbraco.Web.Mvc;
using Web.Helper;
using Web.Model.NullCheck;

namespace Web.Core.Controllers
{
    public class CheckForNullsController : SurfaceController
    {
        public ActionResult Index()
        {
            var currentPage                 = CurrentPage.DocumentTypeAlias;
            var umbContent                  = UmbracoAssignedContentHelper.PageContentByAlias(currentPage);
            string displayImage             = string.Empty;
            string doesImageMediaHaveValue  = umbContent.GetPropertyValue<string>("testMediaImage", "NA");

            if (int.TryParse(doesImageMediaHaveValue, out _))
            {
                displayImage = umbContent.GetPropertyValue<IPublishedContent>("testMediaImage").Url;
            }

            var model = new CheckForNullViewModel
            {
                TestString  = umbContent.GetPropertyValue<string>("testString","I have no value"),
                TestRte     = umbContent.GetPropertyValue("testRte", new HtmlString("RTE has no value")),
                TestImage   = displayImage
            };

            return PartialView("~/Views/Partials/pvCheckForNulls.cshtml",model);
        }
    }
}

View Code

Partials View Code The code for the partial view is below.

@inherits UmbracoViewPage<Web.Model.NullCheck.CheckForNullViewModel>
<div class="container">
    <div class="row">
        <div class="col-md-12">
            <p>Value of test string is: @Model.TestString</p>
            <hr/>
            <div>
                <h2>Value of RTE</h2>
                @Model.TestRte
            </div>
            <hr/>
            @if (!string.IsNullOrEmpty(Model.TestImage))
            {
                <img src="@Model.TestImage" />
            }
            else
            {
                <p>Image has no value</p>
            }
        </div>
    </div>
</div>

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.

Blog Form

 Please complete the required fields (*required)

 *
*