I'm attempting to use Sitecore's Field.HasValue
property in Razor syntax to test a particular field, but no matter what I try, the field always seems to be false.
- I'm using Sitecore 8.
- The Sitecore field, "Header Number" is a Single-Line Text field
Here's what I'm trying:
@{
var phoneNumber = "";
var numberField = Model.Item.Fields["Header Number"];
if (numberField != null && numberField.HasValue)
{
phoneNumber = numberField.Value;
}
}
As you can see in the screenshot below:
numberField
is being correctly set to a Sitecore FieldnumberField.HasValue
is reportingfalse
- However
numberField.Value
is (correctly) returning the value of the field - Because of the failure to fire the
if
block,phoneNumber
is never set:
Is this a bug? Am I using HasValue
incorrectly or is there another Sitecore method I should be using to safely test if fields have a value?
!String.IsNullOrEmpty(numberField)
in yourif
block ? – Dentition.HasValue
on elsewhere. – Valence