Full-Stack freelance Umbraco developer in the UK.

George Phillipson - Freelance Umbraco developer

Umbraco Check For Null Property Value

Umbraco check for null property values
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; }
    }
}
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);
        }
    }
}
@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>
Click link to check values

Please enter your comment.